- substr: documentation ( source)
- substr_count: documentation ( source)
- strpos: documentation ( source)
- array_reverse: documentation ( source)
<?php
$str = "Hello World";$spaceCount = substr_count($str, " ");
$letterIndx = 0;
// count number of spaces and then loop
for($i=0; $i<=$spaceCount; $i++) {
// get space positions
$spaceIndx = strpos($str, " ", $letterIndx);
// assign word by specifying start position and length
if ($spaceIndx == 0) {
$word = substr($str, $letterIndx);
} else {
$word = substr($str, $letterIndx, $spaceIndx - $letterIndx);
}
// push word into array
$myArray[] = $word;
// get first letter after space
$letterIndx = $spaceIndx + 1;
}
// reverse the array
$reverse = array_reverse($myArray);
// echo it out
foreach($reverse as $rev) {
echo $rev." ";
}
?>