Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Display last word from a variable


dreamcatcher - 2:46 pm on Nov 30, 2009 (gmt 0)


Easiest way for last name is to use explode [php.net]

$string = 'James Carpenter Robertson';
$name = explode(' ',$string);
echo $name[count($name)-1)];

You can also use strrpos [php.net] to get the last occurence of a space. Combine this with substr [php.net] and strlen [php.net]:

$string = 'James Carpenter Robertson';
echo substr($string,strrpos($string, ' '),strlen($string));

dc


Thread source:: http://www.webmasterworld.com/php/4033999.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com