Jump to content

Truncate suffixes from a person's name


pakenney38

Recommended Posts

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;
?>

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.