【问题标题】:PHP parse HTML empty input valuePHP解析HTML空输入值
【发布时间】:2015-09-07 10:58:41
【问题描述】:

我知道在 PHP 中解析 HTML 有很多问题,但我似乎找不到我遇到的具体问题。我的代码适用于页面中的其他元素,并且还遍历返回标签名称的输入。同时它们的 value 属性为空,当其中 2 个肯定有值时。这是我的代码

$html = file_get_contents('http://...sample website...html');
$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/input[@type='hidden']");
if(!is_null($elements)){
    foreach ($elements as $element) {
      echo "<br/>[". $element->nodeName. "]";
      echo $element->nodeValue. "\n";
    }
}

【问题讨论】:

    标签: php html xml xpath


    【解决方案1】:
    $xpath->query("//*/input[@type='hidden']/@value");
    

    而不是

    $xpath->query("//*/input[@type='hidden']");
    

    也很好用。

    Same question, same answers

    【讨论】:

      【解决方案2】:

      我自己搞定了,如果其他人有类似的问题,只是 nodeValue 返回一个元素的“innerHTML”,要获取它的属性,请使用$element -&gt; getAttribute("value")(对于“value”属性)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 2013-12-24
        • 2013-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多