Grant Adkins
Grant Adkins

Reputation: 99

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/>";
 }
 }







?>

Upvotes: 2

Views: 1025

Answers (4)

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

This might be exactly what you want:

<?php 
$your_input_string ="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 ";

$your_result = array_chunk(array_filter( array_map('trim', preg_split('/\b(\d+)\b/', $your_input_string, -1, PREG_SPLIT_DELIM_CAPTURE)), 'strlen'),4);

echo '<pre>';
print_r($your_result);
 ?>

Live Demo Here>>

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173622

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');

Upvotes: 1

exussum
exussum

Reputation: 18558

Maybe

<?php 
$rssLine="San Diego 4 Chicago Cubs 2";
//add code to loop though lines
if(preg_match ("/^(.*?) ([0-9]{1,3}) (.*?) ([0-9]{1,3})$/" ,$rssLine, $matches) ===1){

$home_team      = $matches[1];
$home_score     = $matches[2];
$away_team      = $matches[3];
$away_score     = $matches[4];

}
else{
//log no match found
}

?>

Match 1 is home team. Match 2 is home score. Match 3 is away team. Match 4 is away score

Upvotes: 0

Ben
Ben

Reputation: 57267

$arr = array("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"
);

$results = array();

foreach ($arr as $v) {
    $scores = preg_split("/[A-Za-z\s\.]+/", $v);
    $teams = preg_split("/[\d]+/", $v);

    $results[] = "Home: ".$teams[0]." (".$scores[1]."), Away: ".$teams[1]." (".$scores[2].")"; }

foreach ($results as $v) {
    echo $v."<br>"; }

Results:

Home: San Diego (4), Away: Chicago Cubs (2)

Home: Miami (2), Away: Philadelphia (7)

Home: Boston (3), Away: Toronto (1)

Home: Washington (3), Away: Atlanta (1)

Home: Chicago Sox (3), Away: Texas (1)

Home: St. Louis (6), Away: Milwaukee (5)


You could obviously construct $results however you wish; but the meat of the solution is the regexes:

$scores = preg_split("/[A-Za-z\s\.]+/", $v);
$teams = preg_split("/[\d]+/", $v);

Upvotes: 0

Related Questions