0

php。我正在尝试从这个 rss 提要中拆分结果,以便在我网站的不同区域使用,但我似乎得到的只是Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

如果没有 preg_match 代码,它会返回这些结果

Tampa Bay 6 Florida 5 (FINAL - OT)
Ottawa 0 Toronto 3 (FINAL)
Philadelphia 1 Montreal 4 (FINAL)
New Jersey 1 NY Islanders 5 (FINAL)
Anaheim 3 Nashville 2 (FINAL - 2ND OT)
Columbus 3 Phoenix 5 (FINAL)
Colorado 4 Edmonton 6 (FINAL)

正如你所知道的,我不能使用空格来拆分这些,因为某些团队名称有多个空格,所以我使用了这个

preg_match("/^(\D+)(\d+)(\D+)(\d+)\s*\((.*)\)$/", $string, $result);

print_r($result);

$first_team = 1;
$first_team_score = 2;
$second_team = 3;
$second_team_score = 4;
$final = 5; 

正如我上面所说,这将返回空白数组。这是我下面的整个文件。

<?

$sports = array(  
"NHL" => "xml feed here");  
$results = array();  
foreach ( $sports as $sport => $url ) {  
  //get the page pointed to by $url  
    $page = file_get_contents($url);  
    //grab all variables out of the page  
    preg_match_all("/&([^=]+)=([^&]+)/", urldecode($page), $foo);  
    //loop through all the variables on the page  
    foreach ( $foo[1] as $key => $value ) {  
      //debug output, you can delete this next line  
      //echo "{$value} = {$foo[2][$key]}\t<br />\n";  
    //this chain of IF/elseif statements is used to determine which pattern to use  
    //to strip out the correct data, since each sport seems to have its own format  
    //for the variables you'd "want"  
    if ( $sport == "NHL" && preg_match("/s_left\d+/", $value) ) {  
      $results[$sport][] = $foo[2][$key];  
    }   
    }    

}  

//calculate the sport with the most number of rows  
$limit = 0;  
foreach ( $results as $countMe ) {  
  $limit = max($limit, count($countMe));  
}  


//spit out the table with the right headers  
echo "<div id='content'>" . implode( array_keys($sports));  
//loop until you reach the max number of rows, printing out all the table rows you want  
for ( $p = 0; $p < $limit; $p++ ) {  

//echo "{$results[$sport]}";

if(isset($results[$sport][$p])){
echo $scores;
}

$string = $scores;
$result;

preg_match("/^(\D+)(\d+)(\D+)(\d+)\s*\((.*)\)$/", $string, $result);

print_r($result);

$first_team = 1;
$first_team_score = 2;
$second_team = 3;
$second_team_score = 4;
$final = 5; 


echo "{$result[$first_team]}";

//foreach ( array_keys($sports) as $sport ) {  
 //   $results[$sport][$p] = str_replace( '^', '', $results[$sport][$p] ); 
  //  echo "<div id='content1'>{$results[$sport][$p]}</div>";  

 // }  

}  

//kill the main div  

echo "</div>";   

?>
4

1 回答 1

0

首先你需要preg_match_all,因为你想“捕捉”每场比赛

其次,您需要正m则表达式中的修饰符,这意味着“多行”

http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php http://www.php.net/manual/en/function.preg-match-all.php

希望对您有所帮助...

<?

$string = "Tampa Bay 6 Florida 5 (FINAL - OT)
Ottawa 0 Toronto 3 (FINAL)
Philadelphia 1 Montreal 4 (FINAL)
New Jersey 1 NY Islanders 5 (FINAL)
Anaheim 3 Nashville 2 (FINAL - 2ND OT)
Columbus 3 Phoenix 5 (FINAL)
Colorado 4 Edmonton 6 (FINAL)";

preg_match_all("/^(\D+)(\d+)(\D+)(\d+)\s*\((.*)\)$/m", $string, $result);

echo "<pre>";

print_r($result);
于 2013-02-17T17:35:51.183 回答