PHP Regex correct syntax preg_split after any number of words and 1-3 digits -
i'm trying break down rss feed 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 gives me 1 flowing string san diego 4 chicago cubs 2 , i'm trying break down better use.
basically im trying first split san diego 4 chicago cubs 2 4 variables, $home_team, $home_score, $away_team, $away_score.
but, home team 1 word or more, score 1 digit or 3 i've been trying figure out best regular expression split in correct format.
does have ideas?
update
code i'm using for, i'm pulling xml of mlb games today, filtering out games labeled final meaning final score , im trying break down further there..
<?php $xml = simplexml_load_file("http://feeds.feedburner.com/mpiii/mlb?format=xml"); foreach($xml->channel->item $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/>"; } } ?>
you can split string based on sequence of digits, assuming team names don't contain digits :)
$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');
Comments
Post a Comment