【发布时间】:2015-06-30 13:58:48
【问题描述】:
我正在尝试使用我上传到我的网络主机的 php 文档来解析一些 html。当我尝试这个(最后一个回声只是为了看看它是否有效):
<?php
//a URL you want to retrieve
$my_url = 'http://pointstreak.com/prostats/standings.html?leagueid=49&seasonid=12983';
$html = file_get_contents($my_url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
//Put your XPath Query here
$my_xpath_query = "//div[@id='statscontainer']/table/tr/td/table[@class='tablelines']/tr/td";
$result_rows = $xpath->query($my_xpath_query);
// Create an array to hold the content of the nodes
$standingsArray = array();
//here we loop through our results (a DOMDocument Object)
foreach ($result_rows as $result_object){
$standingsArray[] = $result_object->childNodes->item(0)->nodeValue;
}
// Remove the first 12 observations from $standingsArray (table headers)
for ($i = 0; $i < 12; $i++) {
unset($standingsArray[0]);
$standingsArray = array_values($results_rows);
}
// Remove the 12 observations at index 96 (table headers)
for ($i = 0; $i < 12; $i++) {
unset($standingsArray[96]);
$standingsArray = array_values($results_rows);
}
foreach ($standingsArray as $arrayValue) {
echo $arrayValue;
}
echo “HEYHEY”;
?>
我网页上的输出是: “嘿嘿”
但是,如果我换行
foreach ($standingsArray as $arrayValue) {
echo $arrayValue;
}
到:
foreach ($standingsArray as $arrayValue) {
echo "$arrayValue";
}
那么即使是 “——嘿嘿——” 消失了,我只有一个空白网页。
【问题讨论】:
-
在您打开
<?php标记error_reporting(E_ALL); ini_set('display_errors', 1);后立即将错误报告添加到文件顶部,就像@Jite 所说的那样。 -
这不是 php 错误。你有一个字符集不匹配。例如将 utf-8 文本转储到 iso-8859 显示环境中。
-
看起来你对 HEYHEY 的引用有点时髦。我认为您有左双引号而不是常规引号。请参阅此stackoverflow.com/questions/18735921/…。另外,我认为您尝试上传的文档是空的,或者没有得到它。
-
如何解决我的字符集不匹配问题?
标签: php parsing dom html-parsing