php Move the first letter of the words in a valid sentence and use them to replace the first letter of the following word. The first letter of the last word will replace the first letter of the first word in php
i need this answer :-
iorem Lpsum Is iimply summy dext tf ohe trinting pnd aypesetting tndustry
but i getting this answer:-
Lpsum Is iimply summy dext tf ohe trinting pnd aypesetting tndustry i
function myFunction($str,$sString){
$str = str_split($str);
$sString = str_split($sString);
$sString[0] = $str[0];
return implode('',$sString);
}
$array = explode(" ", "Lorem Ipsum is simply dummy text of the printing and typesetting industry");
$array_out = [];
foreach($array as $key => $lijst){
if (strlen($lijst) > 1)
$array_out[] = myFunction($lijst,$array[$key+1]);
else
$array_out[] = $lijst;
}
echo implode(" ", $array_out);
2
Answers
There’s many different ways to solve this. Here, I am putting the first letters of each word into one array, the rest into another. (Using
mb_
string functions, makes this work with UTF-8 & letters outside of the base Latin range.)Then I "rotate" the first letters array by one position, using
array_pop
andarray_unshift
, and then the result gets combined again.