Php 空预匹配数组
PHP。我正试图将此rss提要的结果拆分,以便在我的站点的不同区域使用,但我似乎只得到了Php 空预匹配数组,php,arrays,preg-match,Php,Arrays,Preg Match,PHP。我正试图将此rss提要的结果拆分,以便在我的站点的不同区域使用,但我似乎只得到了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) Anahe
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>";
?>
首先,你需要preg\u match\u all
,因为你想“捕获”每一个匹配
其次,您需要regexp中的m
修饰符,它表示“多行”
希望能对你有所帮助
<?
$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);