Split Name in PHP and Javascript

Split Name in PHP and Javascript


If you have a user with a long name and want to short it because it wont 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."<br/>";
echo $middle_name."<br/>";
echo $last_name;
?>
The above code would print out :
Paul
Steve
Panakkal
There you go! Now Lets do this with JavaScript :
<script>
var name="Paul Steve Panakkal";
var parts=name.split(" ");
first_name=parts[0];
middle_name=parts[1];
last_name=parts[2];
document.write(first_name+"<br>"+middle_name+"<br>"+last_name);
</script>
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.

download file now

Comments

Popular posts from this blog

Spider Man Friend Or Foe ISO Free Download PPSSPP Setting

Sorry Mutter closed unexpectedly on Ubuntu 10 10

Solution Ubuntu 10 04 minimize maximize and close button to right