PHP xpath 搜索查询 [英] PHP xpath search query

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

问题描述

在另一个问题的帮助下,我使用 xpath 从网站返回查询,但我需要它来搜索特定标题.

With help from another question I have used xpath to return queries from a website but I need it to search for specific titles.

这是我目前的代码:

<?php
libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTMLFile("http://www.example.com");
$xpath = new DomXPath($dom);
$nodes = $xpath->query("//span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas')][1]");

foreach ($nodes as $i => $node) {

echo "Node($i): ", $node->nodeValue, "\n";
}

?>

这是一个示例 HTML:

This is a sample HTML:

<td colspan="2" align="center">
  <a href="product.html" style="text-decoration:none">
   <span class="newprodtext">Nike Shoes</span>
  </a>
</td>
<td colspan="2" align="center">
  <a href="product.html" style="text-decoration:none">
   <span class="newprodtext">Nike T-Shirt</span>
  </a>
</td>
<td colspan="2" align="center">
  <a href="product.html" style="text-decoration:none">
   <span class="newprodtext">Adidas Shoes</span>
  </a>
</td>

我需要搜索来查找两个单独的词,例如,如果我在寻找Adidas Shoes",我希望查询返回 TRUE,但如果找到反向词Shoes Adidas",我也希望它返回 TRUE小写shoes adidas"而不是确切的字符串.

I need the search to look for two individual words such as if I was looking for "Adidas Shoes" I want the query to return TRUE but I also want it to return TRUE if it finds the reverse term "Shoes Adidas" or lowercase "shoes adidas" rather than exact string.

推荐答案

只需将您的 xpath 更改为如下所示:

Just change your xpath to something like this:

//span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas shoes')]

如果你想要包含你的搜索词的节点,以及这个

if you want the nodes containing your search word, and to this

translate(//span[@class='newprodtext']/text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='adidas shoes' or `translate(//span[@class='newprodtext']/text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='shoes adidas'

如果您想要 T/F 响应.这里有两种不同的方法,一种使用 contains(),另一种只使用 translate().如果您想要adidas shoes"和shoes adidas"的精确匹配,请使用后一种解决方案.如果您只是在寻找adidas",请使用第一个解决方案,如果您想要 T/F,则应用为后者所做的相同更改.

if you want a T/F response. There's two differing methods here, one using contains() and one using just translate(). If you want an exact matching of 'adidas shoes' and 'shoes adidas' then use the latter solution. If you are just looking for 'adidas', use the first solution and apply the same change made for the latter if you want T/F.

更新:如果你想要只是带有adidas"的第一个节点,那么使用这个:

Update: If you want just the first node with "adidas", then use this:

//span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas')][1]

如果你想要它在 T/F 包装一个 boolean(//span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas')][1]) 围绕它.如果您碰巧需要其他搜索词,请将 contains() 部分复制并粘贴回,并将 adidas 更改为该词.

And if you want it in T/F wrap a boolean(//span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas')][1]) around it. If you do happen to need additional search words, copy and paste the contains() part back in, and change adidas to the word(s).

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

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