PHP。我正在尝试将此rss Feed中的结果拆分为在我网站的不同区域中使用,但我似乎得到的只是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>";
?>
答案 0 :(得分:0)
首先你需要preg_match_all
,因为你想“捕捉”每场比赛
其次,你需要regexp中的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);