Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7046009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:35:58+00:00 2026-05-28T02:35:58+00:00

I am stuck with a NAME field, which typically is in the format: FirstName

  • 0

I am stuck with a NAME field, which typically is in the format:

FirstName LastName

However, I also have the occasional names that are in any of these formats (with prefix or suffix):

Mr. First Last
First Last Jr.

What do people think is a safe way to split these into FIRST/LAST name variables in PHP? I can’t really come up with anything that tends to work all of the time…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:35:59+00:00Added an answer on May 28, 2026 at 2:35 am

    A regex is the best way to handle something like this.
    Try this piece – it pulls out the prefix, first name, last name and suffix:

    $array = array(
        'FirstName LastName',
        'Mr. First Last',
        'First Last Jr.',
        'Shaqueal O’neal',
        'D’angelo Hall',
    );
    
    foreach ($array as $name)
    {
        $results = array();
        echo $name;
        preg_match('#^(\w+\.)?\s*([\'\’\w]+)\s+([\'\’\w]+)\s*(\w+\.?)?$#', $name, $results);
    print_r($results);
    }
    

    The result comes out like this:

    FirstName LastName
    Array
    (
        [0] => FirstName LastName
        [1] => 
        [2] => FirstName
        [3] => LastName
    )
    Mr. First Last
    Array
    (
        [0] => Mr. First Last
        [1] => Mr.
        [2] => First
        [3] => Last
    )
    First Last Jr.
    Array
    (
        [0] => First Last Jr.
        [1] => 
        [2] => First
        [3] => Last
        [4] => Jr.
    )
    shaqueal o’neal
    Array
    (
        [0] => shaqueal o’neal
        [1] => 
        [2] => shaqueal
        [3] => o’neal
    )
    d’angelo hall
    Array
    (
        [0] => d’angelo hall
        [1] => 
        [2] => d’angelo
        [3] => hall
    )
    

    etc…

    so in the array
    $array[0] contains the entire string. $array[2] is always first name and $array[3] is always last name.
    $array[1] is prefix and $array[4] (not always set) is suffix.
    I also added code to handle both ‘ and ’ for names like Shaqueal O’neal and D’angelo Hall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with a database which is using multi-field primary keys. I have a
I have a display name field which I have to validate using Ruby regex.
I've read that combo boxes cannot have multiple columns. Which leaves me a bit
I have this table which contains a field called 'icon_for' which I have been
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name
Being stuck with a legacy database schema that no longer reflects your data model
Kinda stuck here... I have an application with lets say 5000 rows of data
I have a Lucene index that has several documents in it. Each document has
yet another PHP question which I am stuck on, so here is what im
I have a pretty large form that consists of radio buttons/checkboxes as well as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.