Reputation: 19
I have two types of lines that are being pulled from RSS. Those are the games being played and the games that haven't started. The code I have below only pulls the games that have started.
$string = "Tampa Bay 6 Florida 5 (FINAL - OT)
Ottawa 0 Toronto 3 (FINAL)
NY Islanders at Ottawa (7:30 PM ET)
Toronto at Tampa Bay (7:30 PM ET)
San Jose at St. Louis (8:00 PM ET)
Detroit at Nashville (8:00 PM ET)
Vancouver at Chicago (8:30 PM ET)
Los Angeles at Edmonton (10:00 PM ET)";
preg_match_all("/^(\D+)(\d+ )(\D+)(\d+ )\s*\((.*)\)$/m", $string, $result);
echo "<pre>";
print_r($result);
The important thing is that the teams remain in the same array for both types of lines. I have no idea what to do. Any help? I'm sure its just an expression I'm forgetting.
Upvotes: 0
Views: 405
Reputation: 4976
Phew. Here's what I came up with. It uses conditional patterns to match the scores if there are any, and it will sneakily "remove" the the 'at' if no scores are found (indicating it is an unstarted match). It will use the same technique for the second team's score. lastly it will match the contents of the last paranthesis group.
Captured groups:
Contents of last paranthesis
preg_match_all( '#^([a-zA-Z ]+)(?(?=\d+)(\d+)\s+|\s+at\s+)([a-zA-Z ]+)(?(?=\d+)(\d+))\s+(([^)]+))#m', $str, $matches );
// note to edit-happy users, the regex cannot be formatted properly on SO.
The only catch is that you should trim the team name's as there might unwanted be whitespace in them.
Upvotes: 2