Page 1 of 1

[Solved] Splitting Strings

Posted: Sat May 28, 2005 4:09 pm
by davidjwest
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.

Posted: Sat May 28, 2005 4:11 pm
by Revan
You could do explode()

Code: Select all

$data = explode(" ",$_POST["name_variable"]);
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.

Posted: Sat May 28, 2005 4:12 pm
by Burrito
you could split it by the space into an array and then output the array as different strings:

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";

Posted: Sat May 28, 2005 4:13 pm
by JAM
If you can verify that the user uses "First Surname", sure.

Code: Select all

$form_name = "Jam Hood";
list($first, $last) = split(" ", $form_name);
echo $first .' - '. $last;
But it gets tricky if you get a user entering "Jam Sr. Hood".
More info on split.

Gah! Posting to slow. :wink:

Posted: Sat May 28, 2005 4:16 pm
by Burrito
or still use explode and then sizeof() ;)

nah, I'm the slow one today...

damn heat I tell you!

Posted: Sat May 28, 2005 4:19 pm
by JAM
Yah, 28C here... :evil:

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 />";
Result:

Code: Select all

Jam is the first name Sr. is the last name
Jam Sr is the first name Hood is the last name

Posted: Sat May 28, 2005 4:27 pm
by Burrito

Code: 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";
?>
:P

guess you could use array_pop() too, but that'd make it harder to dig out what's in the middle...

Posted: Sat May 28, 2005 4:36 pm
by JAM

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 name
I start to wonder how many variations there are...
And if we scared (unintentionally) away the first poster...

Posted: Sat May 28, 2005 4:36 pm
by davidjwest
Thanks very much!

I remember doing something similar a long long time ago in Spectrum BASIC for an adventure game I wrote.

You had to do a loop through the string examining each character until you got a space, glad PHP is more friendly when it comes to this type of thing!

Posted: Sat May 28, 2005 4:38 pm
by davidjwest
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!

:oops:

Posted: Sat May 28, 2005 4:39 pm
by JAM
Yah, those happy days have passed. :wink:
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.

Posted: Sat May 28, 2005 4:44 pm
by davidjwest
JAM wrote: I start to wonder how many variations there are...
And if we scared (unintentionally) away the first poster...
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 :wink:

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 :D

Posted: Sat May 28, 2005 4:58 pm
by JAM
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 :wink:
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: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 :D
It's often selfexplainatory, not always, but it happens... Glad we could help.

Posted: Sat May 28, 2005 5:12 pm
by davidjwest
Thanks very much everyone, now solved.

Now for my next 1,872,231,321 questions.

:lol:

Posted: Sat May 28, 2005 6:46 pm
by timvw
Instead of going through a pain, and then to discover that for situations like
"Sir Tim Van Wassenhove" it's still not working... I would add another input field on the form and ask surname and familyname separately :p