-4

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
brinardi
  • 47
  • 4

5 Answers5

0

strrev(s) return reversed s

and explode(s) split string s

array_pop( s) return last member of s

<?php
$string ="i like to eat apple";
$pieces = explode(' ', $string);
$last_word = array_pop($pieces);
echo strrev($last_word);
?>
esnkrimi
  • 140
  • 1
  • 8
0

You need to perform 2 steps:

  • Find the last word of the sentence. In case of one word, it returns that word.
$string = 'Retrieving the last word of a string using PHP.';
$last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
$last_word = substr($string, $last_word_start); // $last_word = PHP.

More information here: Get the last word of a string

  • Reverse the string
echo strrev("Hello world!"); // outputs "!dlrow olleH"
Duc Vu
  • 95
  • 1
  • 1
  • 8
0

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;
brinardi
  • 47
  • 4
  • @wscourge why do you edit the answer to "try this". "Try this" is generally not accepted as an explanation on the answer. It wasn't better before, but still? – Andreas Mar 18 '19 at 08:33
  • @Andreas I felt like I should write something, anything really, to accompany the solution. The code itself is self-descriptive enough. What would you suggest? – wscourge Mar 18 '19 at 08:38
  • @wscourge I suggest a comment saying this is not how to answer a question here on SO. All answers should have an description or something that tells the OP what they did wrong. Just replacing one bad thing with another bad thing could be interpreted that "try this" is an acceptable answer. And there is no such thing as self descriptive or self explanatory answer. If there was, there wouldn't be a question either since then OP would already know it all. – Andreas Mar 18 '19 at 09:29
  • @Andreas what I said is that the code is self-descriptive, not the Q/A. Otherwise, I agree, I'll leave the comment next time. Thank you. – wscourge Mar 18 '19 at 09:45
  • No, the code is not self descriptive. There is no such thing. Go to meta and ask if you don't believe me – Andreas Mar 18 '19 at 09:51
0

Isolate that latest occurring sequence of letters (this is a primitive definition of a "word"), then call strrev() on it.

To allow for potential non-letter characters between the last "word" and the end of the string, look (without extending the matched string) for zero or more non-letters immediately before the end of the string.

Code: (Demo)

echo preg_replace_callback(
         '~[a-z]+(?=[^a-z]*$)~i',
         fn($m) => strrev($m[0]),
         $string
     );

Breakdown:

~          #starting pattern delimiter
[a-z]+     #one or more letters
(?=        #start lookahead
  [^a-z]*  #zero or more non-letters
  $        #require the end of the input string
)          #end of lookahead
~          #ending pattern delimiter
i          #case-insensitive pattern flag

Mutations:

  • i like to eat apple becomes i like to eat elppa
  • orange becomes egnaro
  • Surprise! becomes esirpruS!

An alternative pattern is:

~.*\K\b[a-z]+~i

This greedily matches any character until reaching a word boundary then matching the latest occurring sequence of letters. \K means "Keep all matched characters ftom this point onward". The greedy zero-or-more quantifier (*) ensures that only the last "word" is matched.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
$words    = "Reverse string of test";
$wordsArr = explode(" ",$words);
$wordsArr = rsort($wordsArr);
$lastWord = $wordsArr[0];
$reverseWord = strrev($lastWord);
echo "Last Word ::".$lastWord; 
echo "Reverse last Word ::".$reverseWord;