正在分析rss-未定义的偏移量0



我有一个rss解析代码,但我在第38行收到了关于未定义偏移量0的错误消息($$row = $preg_match [1] [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>'."rn";

$rss_file = '';
$fp = fsockopen ('www.soccerstats247.com', 80, $errno, $errstr, 5);
if ($fp) {
    fputs ($fp, "GET /CompetitionFeed.aspx?langId=1&leagueId=1204 HTTP/1.0rnHost: www.soccerstats247.comrnrn");
    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('&lt;br/&gt;&lt;a rel="nofollow" href=""&gt;&lt;/a&gt;'), array(''), $arrData['description']);
    $date = date("Y.m.d", strtotime($arrData['pubDate']));
        echo '<tr><td class="'.$tickerClass.'">'.$date.' '.$result .'<br></td></tr>';
    }
    $lineC++;
}
echo '</table>';
?>

我的问题:为什么我收到错误信息?我的代码出了什么问题?非常感谢。

我已经为您测试了您的代码。如果在循环中var_dump$preg_match,您将看到前3个数组包含提要或网站本身之后的信息。第4个循环只输出一个空数组:

array(2) {
[0] => array(0) { }
[1] => array(0) { }
}

下面是你想知道的球队和比赛数据的实际数组。所以是的。基本上,警告表明在您要查找的数组中没有实际的条目。

最好的做法是

    if(!empty($preg_match[1]))
    {
            $$row = $preg_match [1] [0];
            $arrData[$row] = $$row;
    }

在您尝试输出或维护数据之前。

最新更新