we have that string:
\"I like to eat apple\"
How can I obtain the result \"apple\"
?
How about this get last words or simple get last word from string just by passing the amount of words you need get_last_words(1, $str);
public function get_last_words($amount, $string)
{
$amount+=1;
$string_array = explode(' ', $string);
$totalwords= str_word_count($string, 1, 'àáãç3');
if($totalwords > $amount){
$words= implode(' ',array_slice($string_array, count($string_array) - $amount));
}else{
$words= implode(' ',array_slice($string_array, count($string_array) - $totalwords));
}
return $words;
}
$str = 'I like to eat apple';
echo get_last_words(1, $str);
Get last word of string
$string ="I like to eat apple";
$las_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
$last_word = substr($string, $last_word_start);
echo $last_word // last word : apple