dietkinnie Posted October 12, 2009 Share Posted October 12, 2009 Hi Guys & Gals!! Hope you are all ok.... I need some help with the following I need to trim the following word as follows: $word = 'James Bond'; output would be 'James B' There is a space between all of the words I need to trim... Any suggestion?? Thanks in advance.. Link to comment https://forums.phpfreaks.com/topic/177412-word-trimming/ Share on other sites More sharing options...
Adam Posted October 12, 2009 Share Posted October 12, 2009 Perhaps easiest to use reg exp: <?php $word = 'James Bond'; preg_match('/[a-z]+ [a-z]{1}/i', $word, $match); print $match[0]; ?> Of course, this will only work with strictly a-z characters. Link to comment https://forums.phpfreaks.com/topic/177412-word-trimming/#findComment-935467 Share on other sites More sharing options...
JonnoTheDev Posted October 12, 2009 Share Posted October 12, 2009 <?php $word = 'James Bond'; $word = substr($word,0,strpos($word," ")+2); print $word; ?> Link to comment https://forums.phpfreaks.com/topic/177412-word-trimming/#findComment-935471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.