[Solved] Splitting Strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

[Solved] Splitting Strings

Post 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.
Last edited by davidjwest on Sat May 28, 2005 5:12 pm, edited 1 time in total.
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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";
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

or still use explode and then sizeof() ;)

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

damn heat I tell you!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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!
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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:
Last edited by davidjwest on Sat May 28, 2005 4:42 pm, edited 1 time in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Thanks very much everyone, now solved.

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

:lol:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Post Reply