我可以使用PHP的DOMDocument获得所有<a>
标签,但我如何才能获得<ul>
列表中的标签?我试过$items->childNodes
,没有运气。我应该切换到PHP简单HTML DOM解析器吗?
$string = '
<html>
<head>
</head>
<body>
<p>Please visit my site:
<a href="http://mysite.com/">My Site</a>
</p>
<p>My friend's sites:</p>
<ul>
<a href="http://friend1.com/">Friend 1</a>
<a href="http://friend2.com/">Friend 2</a>
<a href="http://friend3.com/">Friend 3</a>
</ul>
</body>
</html>
';
$DOM = new DOMDocument;
@$DOM->loadHTML($string);
$items = $DOM->getElementsByTagName('a');
for ($i = 0; $i < $items->length; $i++){
echo $items->item($i)->nodeValue;
echo '<br />';
}
使用xpath
$ xpath -> query ("//ul/");
我已经使用xpath构建了相当复杂的选择器。它真的很强大。