Jump to content

split


ki

Recommended Posts

okay im making a submit form for a Full Name so like

 

Full Name [input]

 

 

when people put their full name its usually like

First Last

but some people include their middle like

First Middle Last

 

is there a way I can tell if they put their name like those?

 

when im using list($first, $middle, $last) = split(" ", $_POST['NAME']);

 

Help is appreciated.

Link to comment
Share on other sites

okay cool thanks, i came up with this, tell me if its efficient enough

 

$NE = explode(" ", $_POST[NAME]);
$SPACES = count($NE);
if($SPACES == 2) { list($first, $last) = split(" ", $POST[NAME]); } else { list($first, $middle, $last) = split(" ", $POST[NAME]); }

Link to comment
Share on other sites

that should do it, use quotes around $_POST['NAME'] (watch case also good to be tiddy)  also you might want to do a switch because you have 5 options really

 

$spaces = 0 (No name or only 1 word)

$spaces = 1 (2 names)

$spaces = 2 (3 names)

$spaces = 3 (4 names First, Middle, Last, Titles like Jr/Sr/III)

 

Try and add some textual comments on the input for it and also try and add on some js that filters out nonsense like 4 spaces

Link to comment
Share on other sites

You will also need to add some logic to handle when people only put in one name or if they have more than three names. This will work in all situations:

 

$NE = explode(" ", $_POST[NAME]);

$last   = array_pop($NE);
$first  = array_shift($NE);
$middle = implode(" ", $NE);

Link to comment
Share on other sites

You know you can simply look up each function at www.php.net. But, sure here goes:

<?php

//Create an array of the value split on the space character
$NE = explode(" ", $_POST[NAME]);

// Assign the last element of the array to the variable $last
// AND remove that element from the array
$last   = array_pop($NE);

// Assign the first element of the remaining array to the variable $first
// AND remove that element from the array
//NOTE: If the array has no elements left $first will be null
$first  = array_shift($NE);

// Concatenate all the remianing elements of the array
//  using a space character and assign to $middle.
//NOTE: If the array has no elements left $middle will be null
$middle = implode(" ", $NE);

?>

Link to comment
Share on other sites

MJ thats not a smart idea with the $last = array_pop

because i can write my name

 

John Micheal Doe III

or John Micheal the III Doe

 

somehow you have to determine that last setting based on a match

like if its .jr, Junior, .sr, Senior, III, IV, V, VI (anything greater be pointless) if it matches that then the last is a salutation, otherwise its a lastname

Link to comment
Share on other sites

MJ thats not a smart idea with the $last = array_pop

because i can write my name

 

John Micheal Doe III

or John Micheal the III Doe

 

somehow you have to determine that last setting based on a match

like if its .jr, Junior, .sr, Senior, III, IV, V, VI (anything greater be pointless) if it matches that then the last is a salutation, otherwise its a lastname

Well, then you are getting into much more advanced parsing. There are more than just the examples you provided. For example, I don't know what country you are from, but in the US we add the period at the end of Jr., Sr., etc. Plus there are profesional designations such as MD, DDS, etc. And while were at it, what about the "honorific" that can proceed a name: Mr., Mrs., Miss, etc. And lastly, there is the chance the user will enter the name in the manner Lastname, Firstname.

 

Once you start taking all that into account, it is impossible to create a function that will do it all. No way, no how. My company is in the process of revamping our application and is implementing such functionality which will mimic the parsing logic within MS Outlook. You will never parse names correctly 100% of the time, you just have to make some commen sense assumptions and move on.

 

With that in mind there is one big problem with this solution. It is server-side, after the user has submitted it. That is a bad idea (because you will get some of them wrong) unless you are going to give the user the ability to edit the individual values.

 

I gave the OP a solution that worked based on the parameters he gave. I agree with JP128 that the form should have separate values for each name part if the name needs to be separated into individual values.

 

thats beside the point, php should be able to handle it if you are good enoguh

No, it is not possible for any language to handle name parsing 100% correctly all of the time as I illustrated above.

 

i just realized that the script above doesnt work for some reason, its not returning $first and $last values. any ideaS?

Which script? Mine? I tested it so I know it works.

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.