将一个字符串分成4个不同的字符串?

How could I break apart this string in php?

NY Mets 5 Atlanta 2

Into 4 strings:

$team1
$team1score
$team2
$team2score

I can get the first word but that is it. And even that shouldnt be right, because the NY Mets just pops up as NY.

$words = explode(' ', $gamestring);
$first_word = $words[0];

Some sense of direction would be greatly appreciated.

I don't know what you intend to do, but using explode, you can always do it this way.

You can also try pregmatch and strpos functions to do the same thing.

$gamestring="NY Mets 5 Atlanta 2";
$words = explode(" ",$gamestring);
for($i=0;$i<sizeof($words);$i++){
    echo($words[$i]."<br/>");   
}

You need to define a pattern to look for. For example, you could use this regular expression to look for...

Any number of characters from the start of the string, then a space, then a number, then a space, then any number of characters, then a space, then a number at the end of the string

if (preg_match('/^(.*?) (\d+) (.*?) (\d+)$/', $string, $matches)) {
    $team1 = $matches[1];
    $team1score = $matches[2];
    $team2 = $matches[3];
    $team2score = $matches[4];
}

Demo here - http://codepad.viper-7.com/5LZf4O

using explode will create an array of your string based on the delimiter. You're using the space character, so that's why your first word is just NY instead of NY Mets. If that is your pattern and you want to use, meaning that it will be City, Team #1, Team #1 Score, Team #2, Team #2 score, you can break it up that way and concatenate any elements of the array together to get what you want. So if you wanted the city and team name this way, you would do $team1 = $words[0] . " " . $words[1]; You can print that out and get NY Mets, for example. I hope that helps!

-Frank

Another way to do it using the list(...)

$gamestring = "NY Mets 5 Atlanta 2";
preg_match("/(.*?) (\d+) (.*?) (\d+)/", $gamestring, $matches);
list(, $team1, $team1score, $team2, $team2score) = $matches;

echo "Team 1 = " . $team1 . "<br />";
echo "Score = " . $team1score . "<br /><br />";
echo "Team 2 = " . $team2 . "<br />";
echo "Score = " . $team2score . "<br />";