- strpos: documentation ( source)
<?php
$string = "I am a boy like desired and expected";
for (
$offset = 0, $length = strlen($string);
$offset < $length;
++$offset
) {
$offset += $string[$offset] === ' ';
echo "word at offset $offset, call strpos()\n";
for (
$swaps = 0, $wordEnd = (strpos($string, ' ', $offset) ?: $length) - 1;
$offset < $wordEnd;
++$swaps, ++$offset, --$wordEnd
) {
[$string[$wordEnd], $string[$offset]] = [$string[$offset], $string[$wordEnd]];
echo "$offset : $wordEnd : $swaps : $string\n";
}
$offset += $swaps;
}
echo $string;