Split a name with PHP

I was recently looking for a way to clean up the sign up form for a project I’m working on. I realised I could merge the First name and Last name fields on the form but I needed a way to split the name up afterwards. So after a quick “Google” I came up with a way. The code below splits the name at the first space.

Input: John S Smith
Output: First Name: John Last Name: S Smith

// PHP

$name = "John S Smith";

list($fname, $lname) = split(' ', $name,2);

echo "First Name: $fname, Last Name: $lname";
Tags: , ,

Comments

Hey,

It’s a great tutorial for split description with php but what happen if string having html tags in it.

Here is a tutorial to work with that condition

http://phpschools.freehostia.com/other-stuff/split-html-description-in-php

$name = “Kent Mathisen”;
list($first, $last) = explode(” “, $name);
echo “First name: ” . $first . “, last name: ” . $last;

Won’t give any errors or warnings :)

By
Kent Ivar Mathisen
on

from http://php.net/split :

Note: As of PHP 5.3.0 the regex extension is deprecated, calling this function will issue an E_DEPRECATED notice.

By
John Smith
on

Drop A Comment