Gimple Posted May 1, 2017 Share Posted May 1, 2017 (edited) 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 May 1, 2017 by Gimple Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 1, 2017 Share Posted May 1, 2017 Here is one way. <?php $name = "firstname lastname"; $split_names = explode(" ", $name); echo $split_names[0]; echo $split_names[1]; ?> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 1, 2017 Share Posted May 1, 2017 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 2, 2017 Share Posted May 2, 2017 And what do you do with Ralph Jones Jr. ?? Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 2, 2017 Share Posted May 2, 2017 (edited) 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 May 2, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
Gimple Posted May 2, 2017 Author Share Posted May 2, 2017 Thanks Benanamen! Very helpful. I didn't think the solution was going to be only a few lines of short code! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 2, 2017 Share Posted May 2, 2017 Thanks Benanamen! Very helpful. I didn't think the solution was going to be only a few lines of short code! Because it isn't. At least that is what a co-worker of mine would say. Her first name is "Jo Ann" 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 2, 2017 Share Posted May 2, 2017 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. Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 2, 2017 Share Posted May 2, 2017 Because it isn't. At least that is what a co-worker of mine would say. Her first name is "Jo Ann" Perfect example why you should use separate fields. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.