ki Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/ Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 explode the full name; count number or array elements returned. The zeroth and last element are the name as first name last name Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292250 Share on other sites More sharing options...
ki Posted July 8, 2007 Author Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292252 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292262 Share on other sites More sharing options...
Psycho Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292268 Share on other sites More sharing options...
ki Posted July 8, 2007 Author Share Posted July 8, 2007 okay know u guys are getting me a bit confused, could you explain little bit more? Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292274 Share on other sites More sharing options...
Psycho Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292291 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292300 Share on other sites More sharing options...
ki Posted July 8, 2007 Author Share Posted July 8, 2007 what if i use count_chars() function for the " " and count how many spaces there are? but that also wont determine if the last one is last name or jr or senior Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292316 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 yeah best method is strong guidance on the form and if you really want to try make a if(lastexploded = arrayofsalutations) {$lastexploded = salutation} else {$lastexploded = lastname} Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292318 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 Well, why not just have: <input type='text' name='first'><br> <input type='text' name='middle'><br> <input type='text' name='last'><br> Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292330 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 thats beside the point, php should be able to handle it if you are good enoguh Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292337 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 Yea, well, I was trying to think of an easier method... I hate writing things that I might not need to. Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292339 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 everytime you write a nonsense function save it in a file and document it so you can recall it if needed, I've had so many nonsense like a image resizer that i've used over and over again thinking it be a 1 use thing Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292340 Share on other sites More sharing options...
ki Posted July 8, 2007 Author Share Posted July 8, 2007 i just realized that the script above doesnt work for some reason, its not returning $first and $last values. any ideaS? Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292346 Share on other sites More sharing options...
Psycho Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58902-split/#findComment-292510 Share on other sites More sharing options...
ki Posted July 8, 2007 Author Share Posted July 8, 2007 actually i was talking about my script, but i tested yours and it works just fine Link to comment https://forums.phpfreaks.com/topic/58902-split/#findComment-292543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.