如何使用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/>';
}
}
}
谢谢blasteralfred但我必须使用PHP DOM解析器它。我会尽力找到其他解决方案。但是,谢谢。 – 2012-08-02 09:13:48