我有一个rss解析代码,但是我在第38行($$row = $preg_match [1] [0];
)得到了关于未定义偏移0的错误消息
以下是完整的解析代码:
<?php
echo ' <style>
.scores {font-family:"Trebuchet MS",Verdana, Arial, sans-serif; font-size:12px;line-height:140%; }
.scores th {background:#6D8D9F; text-align:left; border:0; color:#fff;padding:2px 5px 2px 5px;}
.scores td {padding:2px 5px 2px 5px; margin:0; border:0;}
.scores td.dark {background:#D1DADF; }
.scores td.bright {background:#E0E8EF; }
.scores a {color:#ccc; text-decoration:none;}
.scores a:hover {color:#fff; text-decoration:none;}
.scores .foot {text-align:center;background:#6D8D9F; color:#ff0000}
.scores .foot small { color:#222}
.scores small {color:#777;}
</style>'."\r\n";
$rss_file = '';
$fp = fsockopen ('www.soccerstats247.com', 80, $errno, $errstr, 5);
if ($fp) {
fputs ($fp, "GET /CompetitionFeed.aspx?langId=1&leagueId=1204 HTTP/1.0\r\nHost: www.soccerstats247.com\r\n\r\n");
while (!feof($fp)) {
$rss_file .= fgets($fp);
}
fclose($fp);
} else {echo('RSS Error :(');}
$rss_rows = array ( "title", "link", "description", "pubDate" );
$rss_array = explode ( "<item>", $rss_file );
echo '<table class="scores"><tr><th> ';
$lineC = 0;
foreach ( $rss_array as $string ) {
$arrData =array();
foreach ( $rss_rows as $row ) {
preg_match_all ( "|<$row>(.*)</$row>|", $string, $preg_match );
$$row = $preg_match [1] [0];
$arrData[$row] = $$row;
}
$tickerClass = round($lineC/2) == $lineC/2 ? 'dark' : 'bright';
if (stripos($arrData['description'], 'SoccerStats247' ) !== false) {
} else {
$result = str_replace(array('<br/><a rel="nofollow" href=""></a>'), array(''), $arrData['description']);
$date = date("Y.m.d", strtotime($arrData['pubDate']));
echo '<tr><td class="'.$tickerClass.'">'.$date.' '.$result .'<br></td></tr>';
}
$lineC++;
}
echo '</table>';
?>
我的问题:为什么我收到错误消息?我的代码有什么问题?非常感谢。
答案 0 :(得分:2)
我已经为您测试了代码。如果var_dump循环中的$ preg_match,您将看到前3个数组包含feed或网站本身之后的信息。第4个循环只输出一个空数组:
array(2) {
[0] => array(0) { }
[1] => array(0) { }
}
之后是您想要了解的团队和游戏数据的实际数组。是的。基本上警告说,阵列中没有你正在寻找的实际条目。
最好的办法是
if(!empty($preg_match[1]))
{
$$row = $preg_match [1] [0];
$arrData[$row] = $$row;
}
在您尝试输出或维护数据之前。