如何使用PHP DOM解析器获取HTML的特殊属性/自定义属性的值?

问题描述:

<li data-docid="thisisthevaluetoget" class="search-results-item"> 
</li> 

如何获得“data-docid”的值?如何使用PHP DOM解析器获取HTML的特殊属性/自定义属性的值?

您可以使用DOM文档来获得在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/>'; 
     } 
    } 
} 

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

一个例子是here

+0

谢谢blasteralfred但我必须使用PHP DOM解析器它。我会尽力找到其他解决方案。但是,谢谢。 – 2012-08-02 09:13:48