tags:

views:

56

answers:

3

I want to split a string into two variables, the first word and the rest of the string. The first word is only ever going to be one of 4 different words.

$string = explode (' ', $string, 2);
$word = $string[0];
$string = $string[1];

The above seems like it works, but I'm wondering if there is a better way.

+3  A: 

There are many ways you can do it. Using regular expressions, using strtok(), etc. Using explode() the way you are doing is quite fine.

Daniel Egeberg
+6  A: 

You can use list():

list($word, $string) = explode (' ', $string, 2);

But it is already fine. Regular expressions would be overkill in this case.

Felix Kling
@Felix - beat me to the punch
thetaiko
+1  A: 
list($word, $string) = explode(' ', $a, 2);
thetaiko