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:
All comments are held for moderation. We'll publish all comments that are on topic, not rude, and adhere to our Code of Conduct. You'll even get little stars if you do an extra good job.
You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.
Want to tell us something privately, like pointing out a typo or stuff like that? Contact Us.
We have a Code of Conduct.
Be cool. Be helpful. The web is a big place. Have fun. High five.
split() function is deprecated in PHP > 5.3
true and agree with you
@Chriss, better to update it
thanks
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.
Like this:
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
Regards
Immanuel.J
Now time is for using explode() function :).
Easy just do this:
easy!!!
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[0]
$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?Hi
I want to Split the Name ADHITHI C ANEESH as First name and sur name…please help me