【问题标题】:Parsing through PHP, echo not working通过PHP解析,回显不起作用
【发布时间】: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";
    }

那么即使是 “——嘿嘿——” 消失了,我只有一个空白网页。

【问题讨论】:

  • 在您打开&lt;?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


【解决方案1】:

试试这个,

foreach ($standingsArray as $arrayValue) 
{
    echo utf8_encode($arrayValue);
}

更新:

“HEYHEY”似乎是一种不同的字体,请尝试将其删除/替换为普通文本。

【讨论】:

  • 不走运。不过还是谢谢。
  • 上次回显不是普通文本,我在notepad++中打开并删除回显再次输入,运行正常。
  • 天哪,你是对的。你是怎么发现的? notepad++ 会警告你这样的事情吗?我自己永远无法做到这一点。哇谢谢。现在我只需要解决所有这些警告和问题。
  • 没有警告,没有,只是尝试了不同的值,然后我就知道了。
  • 哇。多么奇怪的错误。好吧,谢谢你。我希望我能支持你的评论,但我没有足够的声誉。
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多