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 :
If you have any suggestions/feedback/problems just echo it out in the comments below.
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 :
<?The above code would print out :
$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;
?>
PaulThere you go! Now Lets do this with JavaScript :
Steve
Panakkal
<script>The above code will also print out :
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>
PaulNow you know how to split up names in to first name, middle name and last name in PHP and JavaScript.
Steve
Panakkal
If you have any suggestions/feedback/problems just echo it out in the comments below.
download file now
Comments
Post a Comment