Code Snippet
Separate First and Last Name
$name = "John S Smith";
list($fname, $lname) = split(' ', $name,2);
echo "First Name: $fname, Last Name: $lname";Works with or without middle name.
$name = "John S Smith";
list($fname, $lname) = split(' ', $name,2);
echo "First Name: $fname, Last Name: $lname";Works with or without middle name.
split() function is deprecated in PHP > 5.3
true and agree with you
@Chriss, better to update it
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:
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.