- array_reduce: documentation ( source)
- preg_replace: documentation ( source)
- str_split: documentation ( source)
<?php
$str = "helloworld";
echo preg_replace('~.\L.~', ' ', $str);
echo "\n---\n";
echo array_reduce(str_split($str, 2), function($carry, $item) {
return $carry . $item[0] . (isset($item[1]) ? ' ' : '');
});
echo "\n---\n";
for ($offset = 1, $length = strlen($str); $offset < $length; $offset += 2) {
$str[$offset] = ' ';
}
echo $str;