Jump to content

Truncate suffixes from a person's name


pakenney38

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.