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.


  1. User Avatar
    Permalink to comment#
    $name = "John S. Doe";
    list($fname, $lname) = explode(' ', $name,2);
    echo "First Name: $fname, Last Name: $lname";

    split() function is deprecated in PHP > 5.3

  2. User Avatar
    Permalink to comment#

    Or maybe something more like this to literally get their first and last name, no matter how much junk they enter

    $name = "James Samuel Murphy 'I took that waaaaay too far' Adams";
    list( $fname, $mname, $lname ) = explode( ' ', $name, 3 );		
    if ( is_null($lname) ) //Meaning only two names were entered...
        $lastname = $mname;
        $lname = explode( ' ', $lname );
        $size = sizeof($lname);
        $lastname = $lname[$size-1];
    echo "First Name: $fname, Last Name: $lastname";
  3. User Avatar
    Permalink to comment#

    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);

  4. User Avatar

    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:

    list($fname,$lname) = preg_split('/\s+(?=[^\s]+$)/', $name, 2); 
  5. User Avatar
    Permalink to comment#

    Hi, help me to split my name IMMANUELPRABHU JOSHUA into First name, Middle name & Surname


  6. User Avatar
    Permalink to comment#

    Now time is for using explode() function :).

  7. User Avatar
    Permalink to comment#

    Easy just do this:

    $msgString = "Manpreet Singh Marwaha";
    $result = explode(" ",$msgString);
    echo $result[0]; //Manpreet
    echo $result[1]; //Singh
    echo $result[2]; //Marwaha


  8. User Avatar

    To split the whol full name into first name, middle name and last name.
    $name = “Mike Mad Smith”;

    list($fname,$mname, $lname) = split(‘ ‘, $name,3);

    echo “First Name: $fname, Middle Name: $mname, Last Name: $lname”;

  9. User Avatar

    $name = explode(“ ”, “Mike Mad Smith”);

    $fname = $name[0]
    $lname = end($name);

    echo “First Name: $fname, Last Name: $lname”;

  10. User Avatar
    Permalink to comment#

    What if it’s…

    $name = 'Cal Ripkin, Jr.';

    All these methods will have 'Jr.' as the last name because they assume the last index is the last name, no?

  11. User Avatar
    Revathi V T

    I want to Split the Name ADHITHI C ANEESH as First name and sur name…please help me

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.