PHP Regex correct syntax preg_split after any number of words and 1-3 digits

I'm trying to break down a rss feed with sports scores

Example data

San Diego 4 Chicago Cubs 2
Miami 2 Philadelphia 7
Boston 3 Toronto 1
Washington 3 Atlanta 1
Chicago Sox 3 Texas 1
St. Louis 6 Milwaukee 5 

The rss basically gives me one flowing string like San Diego 4 Chicago Cubs 2 and i'm trying to break it down for better use.

Basically im trying to first split San Diego 4 Chicago Cubs 2 into four variables, $home_team, $home_score, $away_team, $away_score.

But, obviously the home team could be one word or more, the score could be 1 digit or up to 3 so i've been trying to figure out the best regular expression to split this up in the correct format.

Does anyone have any ideas?

Update

Code that i'm actually using this for, i'm pulling xml of mlb games today, filtering out just the games that are labeled as Final meaning Final Score and then im trying to break it down further from there..

<?php





$xml = simplexml_load_file("http://feeds.feedburner.com/mpiii/mlb?format=xml");



  foreach($xml->channel->item as $item){

  if(preg_match('/(FINAL)/', $item->title, $matches) || preg_match('/(POSTPONED)/',   $item->title, $matches)){

if(preg_match('/(POSTPONED)/', $item->title, $matches)){
    continue;
}
$string = $item->title;
$patterns = array();
$patterns[0] = '/\\(FINAL\\)/';
$patterns[1] = '/\\(POSTPONED\\)/';
$replacements = array();
$replacements[1] = '';
$replacements[0] = '';
$string = preg_replace($patterns, $replacements, $string);


 $keywords = preg_match("^(.*?) ([0-9]{1,3}) (.*?) ([0-9]{1,3})$", $string);
 echo $keywords[1]."<br/>";
 }
 }







?>

Answers


You can split the string based on a sequence of digits, assuming that team names don't contain digits as well :)

$s = 'San Diego 4 Chicago Cubs 2';

list($home_team, $home_score, $away_team, $away_score) = array_filter(
  array_map('trim', 
    preg_split('/\b(\d+)\b/', $s, -1, PREG_SPLIT_DELIM_CAPTURE)
  ), 'strlen');

Need Your Help

Rely on multiple tables inside a nested select and a delete MySQL statement

php mysql sql pdo

I am trying to write a dual MySQL query which archives the content of a table to another table, then deletes the original row in the original table.

jQuery page sometimes load, sometimes not

jquery forms submit

As you can see, the javascript makes one validation. It checks if the input have or not text.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.