we have that string:
\"I like to eat apple\"
How can I obtain the result \"apple\"
?
<?php
// your string
$str = 'I like to eat apple';
// used end in explode, for getting last word
$str_explode=end(explode("|",$str));
echo $str_explode;
?>
Output will be apple
.
// 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];
Try this:
$array = explode(' ',$sentence);
$last = $array[count($array)-1];
$str = 'I like to eat apple';
echo substr($str, strrpos($str, ' ') + 1); // apple
a bit late to the party but this works too
$last = strrchr($string,' ');
as per http://www.w3schools.com/php/func_string_strrchr.asp
Try:
$str = "I like to eat apple";
end((explode(" ",$str));