Search code examples
phpreplacesubstringreverse

Reverse the order of letters in the last word in a string


I am trying to reverse the final word in a string.

A multi-word string:

$string = "i like to eat apple";
// to be:  i like to eat elppa

A single word string:

$string = "orange";`
//to be:   egnaro

How can I reverse the order of letters in only the last word -- regardless of how many words are in the string?


Solution

  • I have been Found the answer. For my own question

    $split = explode(" ", $words);
    $v_last = $split[count($split)-1]; 
    $reverse = strrev($v_last);
    $f_word = chop($words,$v_last);
    echo $f_word;
    echo $reverse;