Click to See Complete Forum and Search --> : Extracting the firstname an lastnames


amrigo
07-22-2008, 01:17 PM
Hi

I do receive fullname and want to extract the first name and extract all the part wich is not the firstname.

So for ANTONY LOUIS BUSH

I would like to extract: ANTONY
and LOUIS BUSH

But using strstr does not work to extract the firstname

How can i extract the firstname ?

Thank´s in advance

<?
$nomes = array("FIRSTNAME"=>"ANTONY LOUIS BUSH","LASTNAME"=>"ANTONY LOUIS BUSH");
foreach($nomes as $nome=>$valor){

//print "<br>".$nome." ".$valor;
/*if($nome=="FIRSTNAME"){
$valor=strstr($valor,' ',true);
echo "<br>FIRSTNAME".$valor;
}*/
if($nome=="LASTNAME"){
$valor=strstr($valor, ' ');
echo "<br>LASTNAME: ".$valor;
}
}
?>

SyCo
07-22-2008, 01:36 PM
This will do what you want based on your example but it seems odd you're passing in the same first name and last name values in an array. Why not just send in a string? Is this really what you hope to do? If not tell us the problem you're trying to solve, there may be a better way.

<?
$nomes = array("FIRSTNAME"=>"ANTONY LOUIS BUSH","LASTNAME"=>"ANTONY LOUIS BUSH");
preg_match('/^[a-zA-Z]+/',$nomes['FIRSTNAME'],$arr);
$firstname=$arr[0];
print $firstname.'<br>';

$lastname=trim(str_replace($firstname,'',$nomes['LASTNAME']));
print $lastname;
?>

amrigo
07-22-2008, 02:14 PM
Hy Syco

Yes i agree is odd but this is a situation where i only have this option to receive the data :) .

How does it works ? preg_match find everything that contains letters and stops when find an emprty space? so arr[0] will have the firstname ?

And str_replace ?

Best regards

SyCo
07-22-2008, 02:41 PM
How does it works ? preg_match find everything that contains letters and stops when find an empty space? so arr[0] will have the firstname
yes exactly. preg_match can do a lot more but i gave it the regular expression pattern to find letters (either case) until the first space, which in your example will give the first name.

/ start delimiter
^ from start of string
[a-zA-Z] is a subset of all characters regardless of case (regexp experts, I know there are other ways!! Just keeping it simple :) )
/ end delimiter


String replace then replaces it with nothing (two single quotes together), and inside the trim() removes the first space.

Check the PHP.net manual (use the search for the functions) and read the relevant pages and all the comments.

amrigo
07-24-2008, 09:17 AM
One doubt the expression will work for letters using accents and another kinf orf characters that can be in the name of a person ? eg. áâúd´a

/ start delimiter
^ from start of string
[a-zA-Z] is a subset of all characters regardless of case (regexp experts, I know there are other ways!! Just keeping it simple )
/ end delimiter

NogDog
07-24-2008, 09:21 AM
list($firstName, $lastName) = explode(' ', trim($fullName), 2);

amrigo
07-24-2008, 09:52 AM
Hi nogdog

A fullname can be composed of many names. Eg. i can have: Bill Clinton, as well i can have: Ronald Howard Jackson D´avilla D´eville Jr.

How can i estract just the firstname even if it uses accents ?

Using strstr i will take all the part of the name wich is not the firstname.

Thank´s in advance

NogDog
07-24-2008, 09:58 AM
Did you try my suggestion?

SyCo
07-24-2008, 10:11 AM
I've not tested preg for accents and umlauts although I'm sure there is a regexp for them.

You can explode on the space giving an array of all the name parts regardless how many. The first name will always be $part[0] the first element in the array regardless of accents.

$parts=explode(' ',$nomes['FIRSTNAME']);
$firstname=$parts[0];

You can determine the last name in the same way as my preg suggestion.

$lastname=trim(str_replace($firstname,'',$nomes['LASTNAME']));

You're going to have problems passing in the data like this. eg
is 'John Paul Jones', first name 'John Paul' last name 'Jones' or first name 'john' last name 'Paul Jones'. John Paul is usually considered a first name.

from http://en.wikipedia.org/wiki/John_Paul_Jones
"Leaving Scotland, John Paul commanded a London-registered vessel, the Betsy, "

The data should be collected in the right way or you'll need to manually check every one is right

esm
07-24-2008, 04:22 PM
You're going to have problems passing in the data like this. eg
is 'John Paul Jones', first name 'John Paul' last name 'Jones' or first name 'john' last name 'Paul Jones'. John Paul is usually considered a first name.


My first name is St. Clair. born and raised in South Carolina.

the internet does not like my first name. and government is not too friendly either. :)

While not common, St. Clair is also used as a last name.



.

amrigo
07-29-2008, 07:57 AM
list($firstName, $lastName) = explode(' ', trim($fullName), 2);


Hi nogdog my doubt is: i am inside a foreach usingf list generates two variables ? i think i will need to handle as one variable

amrigo
07-29-2008, 08:26 AM
list($firstName, $lastName) = explode(' ', trim($fullName), 2);


Thanks nog dog i think this is a safe aproach to my problem i will now track the results