PHP xpath包含类,但不包含类 [英] PHP xpath contains class and does not contain class

查看:116
本文介绍了PHP xpath包含类,但不包含类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题进行了总结.我正在尝试在HTML文件中查询所有包含类result且不包含类grid的div标签.

The title sums it up. I'm trying to query an HTML file for all div tags that contain the class result and does not contain the class grid.

<div class="result grid">skip this div</div>
<div class="result">grab this one</div>

谢谢!

推荐答案

这应该做到:

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('test.html');

$xpath = new DOMXPath($doc);
$nodeList = $xpath->query(
    "//div[contains(@class, 'result') and not(contains(@class, 'grid'))]");

foreach ($nodeList as $node) {
  echo $node->nodeName . "\n";
}

这篇关于PHP xpath包含类,但不包含类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆