brem13 Posted April 9, 2009 Share Posted April 9, 2009 hey, im just wondering how i can split text taken from a text field and split it up, like take the first word, then the second. what i wanna do is, enter a first and last name and split it into a separate variable for the first and last name? if anyone can help or point me in the right direction that would be awesome Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Something like: $string = "firstname lastname"; $pieces = explode(" ", $string); echo $pieces[0] . " " . $pieces[1]; Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/#findComment-805868 Share on other sites More sharing options...
nankoweap Posted April 9, 2009 Share Posted April 9, 2009 there may be an easier way, but there's.... http://us.php.net/manual/en/function.strtok.php jason Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/#findComment-805870 Share on other sites More sharing options...
nankoweap Posted April 9, 2009 Share Posted April 9, 2009 Something like: $string = "firstname lastname"; $pieces = explode(" ", $string); echo $pieces[0] . " " . $pieces[1]; yep. that's easier. Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/#findComment-805871 Share on other sites More sharing options...
brem13 Posted April 9, 2009 Author Share Posted April 9, 2009 thank you Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/#findComment-805875 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Be aware that explode will split the string up for any whitespace it sees. So when the string comes in make sure you trim() it and/or check if the pieces aren't blank: if(!empty(trim($pieces[1]))) Quote Link to comment https://forums.phpfreaks.com/topic/153390-solved-take-text-from-field-before-and-after-a-space/#findComment-805879 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.