2013-02-17 139 views
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码因为有些球队的名字有多个空格,所以我用这个

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

?> 

回答

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