我有两个不同的问题:
$string = "i like to eat apple";
输出必须是:我喜欢吃elppa
$string = "orange";
输出必须是:engaro;
因此,如果字符串为单,则输出与字符串相反。但如果字符串不止一个字,只有最后一个字是反向的
strrev(s)返回s
并爆炸分裂字符串s
array_pop(s)返回s的最后一个成员
<?php
$string ="i like to eat apple";
$pieces = explode(' ', $string);
$last_word = array_pop($pieces);
echo strrev($last_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.
更多信息:Get the last word of a string
echo strrev("Hello world!"); // outputs "!dlrow olleH"
我找到了答案。对于我自己的问题
$split = explode(" ", $words);
$v_last = $split[count($split)-1];
$reverse = strrev($v_last);
$f_word = chop($words,$v_last);
echo $f_word;
echo $reverse;