如何获取字符串的最后一个单词

Osc*_*car 3 php

我们有那个字符串:

"I like to eat apple"
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得结果"apple"

Ric*_*ers 18

// Your string
$str = "I like to eat apple";
// Split it into pieces, with the delimiter being a space. This creates an array.
$split = explode(" ", $str);
// Get the last value in the array.
// count($split) returns the total amount of values.
// Use -1 to get the index.
echo $split[count($split)-1];
Run Code Online (Sandbox Code Playgroud)

  • 你可以回显`array_pop($ split)` (4认同)
  • 总结长度检查 (3认同)

use*_*890 5

派对有点迟,但这也有效

$last = strrchr($string,' ');
Run Code Online (Sandbox Code Playgroud)

根据http://www.w3schools.com/php/func_string_strrchr.asp