【问题标题】:How to get the value of special attributes / custom attributes of HTML using PHP DOM Parser?如何使用 PHP DOM Parser 获取 HTML 的特殊属性/自定义属性的值?
【发布时间】:2012-08-02 08:45:11
【问题描述】:
<li data-docid="thisisthevaluetoget" class="search-results-item">
</li>

如何获取“data-docid”的值?

【问题讨论】:

    标签: php parsing dom attributes


    【解决方案1】:

    您可以使用 DOMDocument 获取attributes

    $html = '<li data-docid="thisisthevaluetoget" class="search-results-item"></li>';
    $doc = new DOMDocument;
    $doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('li');
    foreach ($nodes as $node) {
        if ($node->hasAttributes()) {
            foreach ($node->attributes as $a) {
                echo $a->nodeName.': '.$a->nodeValue.'<br/>';
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 JavaScript + jQuery 做到这一点。您可以使用 $_GET 方法获取该值并将其传递到另一个 php 文件中。

      一个例子是 here

      【讨论】:

      • 感谢blasteralfred,但我必须使用PHP DOM Parser。我会尝试寻找其他解决方案。不过谢谢。
      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-05-01
      • 2010-10-29
      • 2011-10-23
      相关资源
      最近更新 更多