Below is the code to find the last word of a string
$pattern = '/[^ ]*$/';
$str = 'Hello world';
preg_match($pattern, $str, $results);
The $results[0] contains last word “world”. If we need to remove or replace the last word use below code.
preg_replace($pattern, '', $str);
Advertisements
Leave a Reply