Lets say I have a variable which equals someones name..
$fullname = "First Last"
Now I want to make a variable which takes the last name from it. Like ..Anything before the space, remove it and create a new var, which would be equal to the last name...
Easy enuf?
TIA!
-Phil
$name = "First Last"; $names = explode(" ",$name); echo $names[0]; echo "<br>"; echo $names[1];
or use the [man]sscanf[/man] function
for example :
$name = "First Last"; $firstname=""; $lastname=""; $names = sscanf($name,"%s %s",&$firstname,&$lastname); echo $firstname; echo $lastname;
Try
list($first,$last) = sscanf($fullname,"%s %s");
or
$nameparts = explode(" ", $fullname); list($first,$last) = $nameparts;
See the reference manual - string functions
hth
First works great thanks!
😃
Erm. This does not account for there being more than one part of the name... for example: Joleeen A. Smith. Or Aaron de Castille.
$personname = "Aaron de castille"; $nameparts = explode(' ',$personname); $lastname = ucfirst($nameparts[count($nameparts)-1]); print $lastname;
Outputs: Castille
$lastname=substr(strrchr($fullname, ' '), 1);
TMTOWTDI 😃
[Of course, this code would mangle John von Neumann's name ... but then, it doesn't work for those names where the surname is written first.]