Code Snippets → PHP → Separate First and Last Name Separate First and Last Name Chris Coyier on Aug 11, 2009 $name = "John S Smith"; list($fname, $lname) = split(' ', $name,2); echo "First Name: $fname, Last Name: $lname"; Works with or without middle name.
split() function is deprecated in PHP > 5.3
true and agree with you
@Chriss, better to update it
Thanks Orves for this suggestion.
Or maybe something more like this to literally get their first and last name, no matter how much junk they enter
Perhaps a better way to go would be to strip multiple whitespace from the name before exploding it.
list($fname,$lname) = explode(‘ ‘, str_replace(‘/\s+/gi’,’ ‘,$name), 2);
Note that with the limit in place, $lname always ends up with the middle names and/or initials. Also, to make this accurate with the limit param in place, you need to ensure that there are no extra spaces. preg_split with a look ahead assertion can solve these two issues with one line of code still:
Hi, help me to split my name IMMANUELPRABHU JOSHUA into First name, Middle name & Surname
Now time is for using explode() function :).
Easy just do this:
To split the whol full name into first name, middle name and last name.
$name = “Mike Mad Smith”;
list($fname,$mname, $lname) = split(‘ ‘, $name,3);
echo “First Name: $fname, Middle Name: $mname, Last Name: $lname”;
$name = explode(“ ”, “Mike Mad Smith”);
$fname = $name
$lname = end($name);
echo “First Name: $fname, Last Name: $lname”;
What if it’s…
All these methods will have
'Jr.'as the last name because they assume the last index is the last name, no?
I want to Split the Name ADHITHI C ANEESH as First name and sur name…please help me