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.. Quote 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. Quote 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/177412-word-trimming/#findComment-935471 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.