Jump to content

How to Separate a Name into Two Strings?


Gimple

Recommended Posts

Hi again,

 

I'm here with another question...

 

I want to be able to let a user type their full name (first and last, separated by a space obviously), in one input field, then I want my code to separate the first and last name into two different strings when it's executed.

 

Anyone have any idea how I would do that?

Edited by Gimple
Link to comment
Share on other sites

I wouldn't recommend this approach. Human names can be a lot more complex than “Firstname Lastname”, and there are no simple rules for extracting the different parts.

 

Do what everybody does and just use multiple fields. Then the user can decide what goes where, because they surely know best.

Link to comment
Share on other sites

Actually, the same code I posted would work with that as well. The Jr. would be in $split_names[2]. Explode is splitting on the spaces.  

 

As @Jaques1 said, this is not a good approach. Separate fields is best. I would provide a dropdown for the suffixes as well as the honorifics (Titles).

Edited by benanamen
Link to comment
Share on other sites

My post was sarcastic since your orig. code did not address more than 2 pieces in the array.

 

As was said, the best way is to grab two inputs (first & last) and manage them separately, thus letting the user determine how his/her name will be presented.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.