我有以下代码
<?php
$doc = new DOMDocument;
$doc->loadhtml('<html>
<head>
<title>bar , this is an example</title>
</head>
<body>
<h1>latest news</h1>
foo <strong>bar</strong>
<i>foobar</i>
</body>
</html>');
$xpath = new DOMXPath($doc);
foreach($xpath->query('//*[contains(child::text(),"bar")]') as $e) {
echo $e->tagName, "\n";
}
Prints
title
strong
i
此代码查找包含单词“bar”的任何 HTML 元素,并且它与具有“bar”的单词(如“foobar”)匹配 我想更改查询以仅匹配单词“bar”,没有任何前缀或后缀
我认为可以通过更改查询来搜索每个前后没有字母或前后有空格的“栏”来解决
这段代码来自过去的问题here https://stackoverflow.com/questions/1231606/how-to-figure-out-the-location-of-a-keyword-in-an-html-document by VolkerK https://stackoverflow.com/users/4833/volkerk
Thanks
您可以使用以下 XPath 查询
$xpath->query("//*[text()='bar']");
or
$xpath->query("//*[.='bar']");
请注意,使用“//”会减慢速度,XML 文件越大。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)