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. Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506616 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. Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506624 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 Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506648 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; ?> Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506660 Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 You learn something new everyday! Ken Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506665 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? Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506669 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); } Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506672 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]. Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506674 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. Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506680 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); ?> Link to comment https://forums.phpfreaks.com/topic/99009-truncate-suffixes-from-a-persons-name/#findComment-506684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.