Simple_Html_Dom解析器无法在函数内部工作

时间:2015-01-30 01:39:11

标签: php parsing file-get-contents simple-html-dom

我试图使用Simple_Html_Dom解析器来解析NBA统计数据的网页。我会有很多不同的网址,但解析相同的数据,所以我想我会创建一个函数。在函数之外,这个解析器工作得很好,但是只要我将解析放在函数中,我就会收到连接错误。只是想知道,如果有人知道为什么我不能在函数内运行file_get_html。这是下面的代码。请帮忙!

include('simple_html_dom.php'); 

$nbaPlayers = 'playerstats/15/1/eff/1-2';

function nbaStats($url){
    $html = 'http://www.hoopsstats.com/basketball/fantasy/nba/';
    $getHtml = $html . $url;
    $a = file_get_html("$getHtml");

    foreach($a->find('.statscontent tbody tr') as $tr){
        $nbaStatLine = $tr->find('td');
        $nbaName = $nbaStatLine[1]->plaintext;
        $nbaGamesPlayed = $nbaStatLine[2]->plaintext;
        $nbaMinuesPlayed = $nbaStatLine[3]->plaintext;
        $nbaTotalPoints = $nbaStatLine[4]->plaintext;
        $nbaRebounds = $nbaStatLine[5]->plaintext;
        $nbaAssists = $nbaStatLine[6]->plaintext;
        $nbaSteals = $nbaStatLine[7]->plaintext;
        $nbaBlocks = $nbaStatLine[8]->plaintext;
        $nbaTurnovers = $nbaStatLine[9]->plaintext;
        $nbaoRebounds = $nbaStatLine[11];
        $nbadRebounds = $nbaStatLine[12];
        $nbaFieldGoals = $nbaStatLine[13];
        $nbaFieldGoals = explode("-", $nbaFieldGoals);
        $nbaFieldGoalsMade = $nbaFieldGoals[0];
        $nbaFieldGoalsAttempted = $nbaFieldGoals[1];
        // Player Stat Line
        $playerStats = $nbaName . ': gp - ' . $nbaGamesPlayed . ' mp - ' . $nbaMinutesPlayed . ' pts - ' . $nbaTotalPoints . ' rb - ' . $nbaRebounds . ' as - ' . $nbaAssists . ' s - ' . $nbaSteals . ' bl - ' . $nbaBlocks . ' to - ' . $nbaTurnovers;

        echo $playerStats . '<br /><br />';
    }
}
nbaStats($nbaPlayers);

1 个答案:

答案 0 :(得分:0)

将变量传递给函数时,不需要将变量包装在引号中。将文本直接传递给函数或将文本分配给变量时使用引号。

改变这个:

$a = file_get_html("$getHtml");

对此:

$a = file_get_html($getHtml);