所以我有一个这样的
HTML字符串:
原文链接:https://www.f2er.com/php/139929.html<td class="name"> <a href="/blah/somename23123">Some Name</a> </td> <td class="name"> <a href="/blah/somename28787">Some Name2</a> </td>
使用XPath我可以使用此Xpath查询获取href属性的值:
$domXpath = new \DOMXPath($this->domPage); $hrefs = $domXpath->query("//td[@class='name']/a/@href"); foreach($hrefs as $href) {...}
并且获得文本值更容易,如下所示:
// Xpath auto. strips any html tags so we are // left with clean text value of a element $domXpath = new \DOMXPath($this->domPage); $names = $domXpath->query("//td[@class='name']/"); foreach($names as $name) {...}