php函数将多个html行添加到变量中

时间:2014-12-23 03:16:45

标签: php html

我试图创建一个包含表的函数,然后遍历数据并添加等于循环执行的行。目前它显示一个空白的页面,并想知道我为了退回桌子而做错了什么?您将无法看到结果,因为需要一个非常大的后端文件,但我希望有人可以看到我输出表格时出错了什么?

$url = "http://URL";
function getHTML($url,$timeout)
{
   $ch = curl_init($url); // initialize curl with given url
   curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
   curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
   return @curl_exec($ch);
}

function getGames() {

$html = str_get_html(getHTML($url,10));

    //$title = str_replace(array("\n", "\r"), '',$html->find("/[@id='main']/div[1]/div[2]/div[1]/h2/strong",0)->plaintext);
    //$manuf = $html->find("/[@id='main']/div[1]/div[2]/div[3]/table/tbody/tr[1]/td[2]/strong",0)->plaintext;
$table = $html->find("/[@id='matches_list']/",0);

$livescore = "<table class='match-table' cellspacing='0' cellpadding='0'>";
$livescore = ."<tbody>";






foreach($table->find("li") as $line){
    $game = $line->find("a/span/img",0)->title;
    if(  $game == "CS:GO" or $game == "Hearthstone" or $game == "Dota 2" or $game == "StarCraft II" or $game == "League of Legends"){

        $opp1 = $line->find("span.opp1",0)->plaintext;
        $opp2 = $line->find("span.opp2",0)->plaintext;
        $score = $line->find("span.score",0)->plaintext;

            $livescore .= "<tr class='margin-tr'>";
            $livescore .= "<td class='match-icon'>Icon</td>";
            $livescore .= "<td class='match-home'>Opp1</td>";
            $livescore .= "<td class='match-score'>score</td>";
            $livescore .= "<td class='match-away'>opp2</td>";
            $livescore .= "<td class='match-time'>22:00</td>";
            $livescore .= "</tr>";

        }

    }

$livescore = ."</tbody>";
$livescore = ."</table>";

return $livescore;

}


echo getGames();

1 个答案:

答案 0 :(得分:1)

您的函数名为getGames(),您尝试使用echo getGame();

访问它

$html = str_get_html(getHTML($url,10)); //<<<< $url isn't set