Split Name in PHP and Javascript


Read {count} times since 2020

If you have a user with a long name and want to short it because it won’t fit into the element that contains the name, you can either do it on the server or the client. In this post I am going to tell you how to accomplish this with PHP and JavaScript.
Suppose We have a user named Paul Steve Panakkal (my cousin). To separate the first name, middle name and last name you can do the following in PHP :

$name="Paul Steve Panakkal";
$parts=explode(‘ ‘,$name);
$first_name=$parts[0];
$middle_name=$parts[1];
$last_name=$parts[2];
echo $first_name."
";
echo $middle_name."
";
echo $last_name;
?>

The above code would print out :

Paul
Steve
Panakkal

There you go! Now Let’s do this with JavaScript :

The above code will also print out :

Paul
Steve
Panakkal

Now you know how to split up names in to first name, middle name and last name in PHP and JavaScript.
If you have any suggestions/feedback/problems just echo it out in the comments below.