Here is another version, using a function.
<?php
function word2star($word){
if(empty($word))
return -1;
for($i = 0; $i < strlen($word); $i++)
$word[$i] = '*';
return $word;
}
$string = 'How much wood would a wood chuck chuck if a wood chuck could chuck wood';
$arr = explode("\x20", $string); // explode into array on spaces (hex 20)
$out2 = array();
$arr2 = array_slice($arr, 0, count($arr)-3);
foreach($arr2 as $key => $val){
$out2[] = word2star($val);
}
$lastThree = array_slice($arr, count($arr)-3, count($arr)); // get last 3 words
$out2 = array_merge($out2, $lastThree); // merge them together
$out2 = implode("\x20", $out2); // implode into string
echo $out2;
?>