【问题标题】:DOM loadhtml extract nodes and child nodesDOM loadhtml 提取节点和子节点
【发布时间】:2015-05-20 05:40:54
【问题描述】:

我有一个项目列表,我需要在其中获取列表标题属性、链接 URL 和显示的链接文本,以及每个列表标记的跨度值。

<ul>
<li class="testclass" title="Title 1 goes here">
<a href="http://examplelink1.com">List Text 1</a>
<span>Second List Text 1</span>
</li>
<li class="testclass" title="Title 2 goes here">
<a href="http://examplelink2.com">List Text 2</a>
<span>Second List Text 2</span>
</li>
</ul>

如何使用 foreach 提取每个单独的列表标签及其值(因为之后我需要将值插入 MySQL 数据库(每个值在不同的数据库字段中)。

到目前为止,我只能分别获得它们:

<?php
$doc = new DOMDocument();
@$doc->loadHTML($list);
$imageTags = $doc->getElementsByTagName('a'); 
foreach($imageTags as $tag) {
$link = $tag->getAttribute('href');
echo $link.'<br/>';
}
?>

<?php
$doc = new DOMDocument();
@$doc->loadHTML($list);
$imageTags = $doc->getElementsByTagName('li'); 
foreach($imageTags as $tag) {
$link = $tag->getAttribute('title');
echo $link.'<br/>';
}
?>

我找到了一个带有 xpath 的脚本,但我不知道如何正确应用它来获取我需要的特定值并在 MySQL 语句中使用它们:

<?php
$dom = new DOMdocument();
@$dom->loadHTML($list);
$xpath = new DOMXPath($dom);
$elements = $xpath->query("//*");
foreach ($elements as $element) {
echo "<p>". $element->nodeName. "</p>";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "<br/>";
}
}
?>

【问题讨论】:

    标签: php dom xpath foreach nodes


    【解决方案1】:

    使用DOMXPath::evaluate()。它是ext/dom 的一部分,允许您使用 XPath 表达式从 DOM 中获取节点和值。

    $dom = new DOMDocument();
    $dom->loadHtml($html);
    $xpath = new DOMXPath($dom);
    
    // use an xpath expression to fetch the li nodes
    foreach ($xpath->evaluate('//ul/li[@class="testclass"]') as $li) {
      var_dump(
        [
          // this is a direct attribute of the li node, use dom method
          'title' => $li->getAttribute('title'),
          // more complex, use an xpath expression
          'href' => $xpath->evaluate('string(a/@href)', $li),
          // Cast the node to a string to return the text content
          'link-text' => $xpath->evaluate('string(a)', $li),
          // works for the span, too
          'description' => $xpath->evaluate('string(span)', $li)
        ]
      );
    }
    

    输出:

    array(4) {
      ["title"]=>
      string(17) "Title 1 goes here"
      ["href"]=>
      string(23) "http://examplelink1.com"
      ["link-text"]=>
      string(11) "List Text 1"
      ["description"]=>
      string(18) "Second List Text 1"
    }
    array(4) {
      ["title"]=>
      string(17) "Title 2 goes here"
      ["href"]=>
      string(23) "http://examplelink2.com"
      ["link-text"]=>
      string(11) "List Text 2"
      ["description"]=>
      string(18) "Second List Text 2"
    }
    

    【讨论】:

    • 哇,太棒了!谢谢!有没有办法只回显没有数组的干净格式的值?我试过 foreach($xpath as $key => $val){echo $val;} 但它似乎不起作用...
    • 好的,我找到了一种方法,只需要直接回显 value 属性而不需要 vardump!哈哈!谢谢您的帮助!非常感谢!
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多