0

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);
sukesh
  • 11
  • 1
  • It's because of `$array[$key+1]` where this is NULL. Try [like this (demo)](https://tio.run/##dZFNT8MwDIbv@RVW1UMjDbZdKWw3BBKHSRyrCpXWXQNpEuUDViF@e3HTbogDuTl@/fqxbTozjrf7w8OBsdR5C3eQPGmLPTwaF3oQDpzojRygCX0/gMeTB92C7xCMFcoLdYRKNeAHgw59jIVqAnkNSU6mlbXVQLZ4MlI3mCWQrGBqxXOYky86eBIUJclb6l3VXbaUVQ7Sd6TyHaRSvDnP2RcDeus1tMI6D5/aNhFAVku0AjVBXhLG4ofQwcWfWCxayAhAosoWV9jBlsfc9H6xipLA@uE@qNoLfZavItRuA/tFWkzx1baEG0DVLPCc59ERpcP/rGe/nH0zRhNZNLKqMS53nk7SRtGydun/B4UmWKXu2dMVjhzmtVj0wSo4fxeb8tqFV1LO8i2PrbDuNExX/b3HBYvn4/gD) – bobble bubble Oct 13 '22 at 20:39
  • 1
    Interesting to solve this with [regex](https://stackoverflow.com/a/22944075/5527985). Not being sure about those single character exceptions I came up with [search for `^\w(?=.*\b(\w))|(?<=(\w))\w*\W+\K\w` and replace with `$1$2` (regex101 demo)](https://regex101.com/r/O9xnWy/1) - Use with [preg_replace (demo)](https://tio.run/##PcyxDoIwFIXhnae4A0kLGomuSFg1OrC5NBqEKzSh5aYtwSa@e0UGp3P@5aOeQjiW1amKotg6AwWw62hQwZnspEBasFLR4KGdlPLg8O1gfIHrEchI7aTuoNYtOE9o0a0tdTstlmf5HyWD3cMgDXWDnGV3MfOy2KXiycWcJB9eHov1iTkVt424iDljW2DxPj4s@0OSBcOmH9fIQ/gC) – bobble bubble Oct 13 '22 at 20:44

2 Answers2

1

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 and array_unshift, and then the result gets combined again.

$words = explode(" ", "Lorem Ipsum is simply dummy text of the printing and typesetting industry");

foreach($words as $word) {
    $firstLetters[] = mb_substr($word, 0, 1);
    $otherLetters[] = mb_substr($word, 1);
}

array_unshift($firstLetters, array_pop($firstLetters));

foreach($firstLetters as $key => $firstLetter) {
    $result[] = $firstLetter . $otherLetters[$key];
}

echo implode(' ', $result);
CBroe
  • 91,630
  • 14
  • 92
  • 150
0
function myFunction($words,$chars)
{
    return $chars.$words;
}

$words = explode(" ", "Lorem Ipsum is simply dummy text of the printing and typesetting industry");

foreach ($words as $key => $value) {

    $cond = ($key>0)?mb_substr($words[$key-1], 0,1):mb_substr(end($words),0,1);

    $result[] = myFunction(mb_substr($value,1),$cond);

}

$finalRes = implode(" ", $result);

echo $finalRes;
sukesh
  • 11
  • 1