php – DOMXpath – 获取一个元素的href属性和文本值[转]

admin6年前IT技术2071

原文https://stackoverflow.com/questions/6820429/domxpath-get-href-attribute-and-text-value-of-an-a-element
       http://www.voidcn.com/article/p-cjeefole-btv.html 

 

所以我有一个这样的 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) {...}

现在我很想知道,如何组合这两个查询来获取两个值只有一个查询(如果这样的话甚至可以吗?).

 

 

 

//td[@class='name']/a

然后用nodeValue和getAttribute(‘href’)的属性提取文本.

除此之外,您可以将Xpath查询与Union Operator |组合所以你可以使用

 

//td[@class='name']/a/@href|//td[@class='name']

 

相关文章

Xray VLESS / TLS

Xray VLESS / TLS

 Amazon Linux 2 上顺利运行的 Xray 一键安装脚本,包含:Xray VLESS / TLS(443 或 8443,建议用 8443)systemd 自动启动BBR + 内核...

shadowsocks_install

执行命令:bash <(curl -sL https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadow...

sublime编译运行PHP

创建PHP编译系统添加 PHP 的 build system,如图所示, Tools->Build System-> New Build System :新建一个,默认的内容是:{&nbs...

IOS14.2无法连接l2tp的解决方案,iphone无法连接l2tp的解决方案,配置服务器以将 SHA-256 哈希的输出截断至 128 位

IOS14.2无法连接l2tp的解决方案,iphone无法连接l2tp的解决方案,配置服务器以将 SHA-256 哈希的输出截断至 128 位

 解决方案其实很简单:以centos 7的l2tp方案为例:修改文件/etc/ipsec.conf里的"sha2-truncbug=yes"为"sha2-tru...

magento1.9x系统加速

1.设置magento数据库模型 为单表查询:   在System > Configuration > Catalog > FrontEnd设置 ‘U...

linux 挂载win7共享文件夹的方法

mount -t cifs -o user=administrator,password=000000 //192.168.1.2/gongxiang /mnt/share...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。