pakenney38 Posted April 1, 2008 Share Posted April 1, 2008 Say I have a string like "JOHN DOE JR". Does anyone know the best way to truncate suffixes like " JR" from the name? I need to be able to truncate " JR", " SR", " I", " II", " III", " IV", etc on a large list of names. The names are stored in MySQL if it matters. Quote Link to comment Share on other sites More sharing options...
trq Posted April 1, 2008 Share Posted April 1, 2008 If they are the only suffixes, the easiest method would be a simple string replace. <?php $val = str_replace(array(" JR", " SR", " I", " II", " III", " IV"),"",$val); ?> Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted April 1, 2008 Author Share Posted April 1, 2008 I tried that and unfortunately there are several names in the list that are multiple words with words beginning with " I", so that will not work. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 You should be able to do something like this: <?php $str = 'JOHN DOE JR'; $tmp = explode(' ',$str); array_pop($tmp); $new_str = implode(' ',$tmp); echo "$str<br>\n$new_str\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
papaface Posted April 1, 2008 Share Posted April 1, 2008 You should be able to do something like this: <?php $str = 'JOHN DOE JR'; $tmp = explode(' ',$str); array_pop($tmp); $new_str = implode(' ',$tmp); echo "$str<br>\n$new_str\n"; ?> Ken If you use PHP5 you can do it like this: <?php $str = 'JOHN DOE JR'; $tmp = implode(' ',explode(' ',$str,-1)); echo $tmp; ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 You learn something new everyday! Ken Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted April 1, 2008 Author Share Posted April 1, 2008 Thanks! That gets me a lot closer. Is there any way I could check to see if $str[2] equals any value within an array? Say I have: $suffixes = array('JR', 'SR', 'I', 'II', 'III', 'IV'); I realize I would have to find the number of elements in $str, but can I just do: $str = 'JOHN DOE JR'; $tmp = explode(' ',$str); if $str[2] == $suffixes { array_pop($tmp); $new_str = implode(' ',$tmp); } Sorry I don't mean to be a complete noob, but how would I work the if statement? Quote Link to comment Share on other sites More sharing options...
trq Posted April 1, 2008 Share Posted April 1, 2008 if (in_array($str[2],$suffixes)) { array_pop($tmp); $new_str = implode(' ',$tmp); } Quote Link to comment Share on other sites More sharing options...
papaface Posted April 1, 2008 Share Posted April 1, 2008 OP - Just so you know, it should be $tmp[2] not $str[2]. Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted April 1, 2008 Author Share Posted April 1, 2008 Good point. Sorry for the confusion. Thanks for the solution. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 1, 2008 Share Posted April 1, 2008 You could also do <?php $val = preg_replace('/ (JR|SR|I{1,3})$/', '', $name); ?> Quote Link to comment 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.