[Solved] Splitting Strings
Moderator: General Moderators
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
[Solved] Splitting Strings
Can someone let me know how to split a string please?
I have a form where the user enters a name and I want to split it into forename and surname.
I suppose I could change the form so they enter the name separately, but is it easy enough to do with PHP?
Thanks.
I have a form where the user enters a name and I want to split it into forename and surname.
I suppose I could change the form so they enter the name separately, but is it easy enough to do with PHP?
Thanks.
Last edited by davidjwest on Sat May 28, 2005 5:12 pm, edited 1 time in total.
You could do explode()
A sort of dodgy way to do it, there are other functions for this, but I can't remember, so someone else will probably say.
Code: Select all
$data = explode(" ",$_POST["name_variable"]);you could split it by the space into an array and then output the array as different strings:
ex:
ex:
Code: Select all
$string = "Bob Smith";
$split = explode(" ",$string);
$first = $split[0];
$last = $split[1];
echo $first." is the first name ".$last." is the last name";If you can verify that the user uses "First Surname", sure.
But it gets tricky if you get a user entering "Jam Sr. Hood".
More info on split.
Gah! Posting to slow.
Code: Select all
$form_name = "Jam Hood";
list($first, $last) = split(" ", $form_name);
echo $first .' - '. $last;More info on split.
Gah! Posting to slow.
Yah, 28C here... 
Continuing with examples... Dunno if this is the best approach, but it at least demonstrates some more:
Result:
Continuing with examples... Dunno if this is the best approach, but it at least demonstrates some more:
Code: Select all
$form_name = "Jam Sr. Hood";
list($first, $last) = split(" ", $form_name);
echo $first." is the first name ".$last." is the last name<br />";
$last_space = strrpos($form_name, " ");
$first = substr($form_name, 0, $last_space -1);
$last = substr($form_name, $last_space+1);
echo $first." is the first name ".$last." is the last name<br />";Code: Select all
Jam is the first name Sr. is the last name
Jam Sr is the first name Hood is the last nameCode: Select all
<?
$string = "Bob Smith Jones";
$split = explode(" ",$string);
$first = $split[0];
$lastnum = (sizeof($split) - 1);
$last = $split[$lastnum];
echo $first." is the first name ".$last." is the last name";
?>guess you could use array_pop() too, but that'd make it harder to dig out what's in the middle...
Code: Select all
$string = "Bob Smith Jones";
$split = explode(" ",$string);
$first = $split[0];
$last = $split[count($split)-1];
echo $first." is the first name ".(count($split) > 2 ? ", ".$split[1] ." is the middle, " : ", ").$last." is the last name";Code: Select all
Bob is the first name , Smith is the middle, Jones is the last nameAnd if we scared (unintentionally) away the first poster...
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
Jeez, here I am trying to write a multi-player web-based real-time game using PHP and MySQL and I can't even get my BB Code to work in my sig!

Last edited by davidjwest on Sat May 28, 2005 4:42 pm, edited 1 time in total.
Yah, those happy days have passed.
Do lookup the various string functions in the manual. Depending on your approach to the matter, there might be many other functions that would suit you better. The above was just some examples.
Do lookup the various string functions in the manual. Depending on your approach to the matter, there might be many other functions that would suit you better. The above was just some examples.
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
No, takes more than that to scare me, but if I knew how complex PHp could get when I first started I'd probably be using ASP or somethingJAM wrote: I start to wonder how many variations there are...
And if we scared (unintentionally) away the first poster...
I thought there'd be a function which would be helpful and it's nice to see a few different ways of doing things!
I even understand some of them
Oh, I that was the end of our friendship then.. J/k. I don't think ASP is less harder to understand if you don't come from the VB family that is.davidjwest wrote:No, takes more than that to scare me, but if I knew how complex PHp could get when I first started I'd probably be using ASP or something
It's often selfexplainatory, not always, but it happens... Glad we could help.davidjwest wrote:I thought there'd be a function which would be helpful and it's nice to see a few different ways of doing things!
I even understand some of them
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England