Obviously, the real code is the content of the second loop. The loops are only there as test harnesses to make sure that the right words are returned for every possible value of $pos.
$sentence = "Dit is een simpele text die nergens op slaat. I'd write this sentence in Dutch if I knew any Dutch.";
// Okay, let's test this.
for($pos = 0; $pos<strlen($sentence); ++$pos)
echo $pos,' ',array_pop(explode(' ',substr($sentence,0,$pos))),"\n";
// Not what is desired, I take on.
// Regexps? Okay, let's have a crack with them.
for($pos=0; $pos<strlen($sentence); ++$pos)
{
// Break the sentence into to pieces at $pos. The word we want will be the
// last one on the left. If $pos was inside the word at the time, a fragment
// of the word will end up on the right.
$left = substr($sentence, 0, $pos);
$right = substr($sentence, $pos);
// Isolate the last word to the left of $pos and the first word to the right of $pos.
// There may be some whitespace after the last word, and there may be some before the first.
preg_match('/(\\S*)(\\s*)$/', $left, $last_word_and_space);
preg_match('/^(\\s*)(\\S*)/', $right, $space_and_first_word);
// We don't need $junk - it's the entire string matched - word and space and all.
list($junk, $last_word, $space_on_left) = $last_word_and_space;
list($junk, $space_on_right, $first_word) = $space_and_first_word;
if($space_on_left=='' && $space_on_right=='')
{
// $pos lay inside a word, which got bro
// ken into two pieces.
$word = $last_word.$first_word;
}
elseif($space_on_right!='')
{
// $pos was in whitespace
// at the end of a word
$word = $last_word;
}
elseif($space_on_left!='') // Don't really need to test this
{
// $pos was positioned
// at the start of the word
$word = $first_word;
}
echo $pos,' ',$word,"\n";
}
(Edit: just noticed vBulletin had turned my \s's and \S's into s's and S's. That's not right.)