我可以使用phpQuery查找精确匹配吗?
我用phpQuery制作了一个脚本。脚本将查找包含特定字符串的td:我可以使用phpQuery查找精确匹配吗?,php,jquery,phpquery,Php,Jquery,Phpquery,我用phpQuery制作了一个脚本。脚本将查找包含特定字符串的td: $match = $dom->find('td:contains("'.$clubname.'")'); 到目前为止,效果还不错。因为一个俱乐部的名字是奥地利卢塞努,第二个俱乐部是卢塞努。它将选择两个俱乐部,但它只应该选择Lustenau(第二个结果),所以我需要找到一个包含精确匹配的td 我知道phpQuery正在使用jQuery选择器,但不是全部。有没有办法使用phpQuery找到精确的匹配项?我没有phpQuer
$match = $dom->find('td:contains("'.$clubname.'")');
到目前为止,效果还不错。因为一个俱乐部的名字是奥地利卢塞努,第二个俱乐部是卢塞努。它将选择两个俱乐部,但它只应该选择Lustenau(第二个结果),所以我需要找到一个包含精确匹配的td
我知道phpQuery正在使用jQuery选择器,但不是全部。有没有办法使用phpQuery找到精确的匹配项?我没有phpQuery的经验,但是jQuery应该是这样的:
var clubname = 'whatever';
var $match = $("td").map(function(index, domElement) {
return ($(domElement).text() === clubname) ? domElement : null;
});
phpQuery文档表明->map()
可用,并且它以与jQuery相同的方式接受回调函数
我相信您将能够执行phpQuery的翻译
编辑
以下是我基于5分钟阅读的尝试-可能是垃圾,但以下是:
$match = $dom->find("td")->map(function($index, $domElement) {
return (pq($domElement)->text() == $clubname) ? $domElement : null;
});
编辑2
这是一个例子
如果phpQuery按照它在其中所说的做,那么(正确地从javascript翻译过来)它应该以相同的方式匹配所需的元素
编辑3
在进一步阅读有关的内容后,以下代码更有可能工作:
function textFilter($i, $el, $text) {
return (pq($el)->text() == $text) ? $el : null;
}};
$match = $dom->find("td")->map('textFilter', new CallbackParam, new CallbackParam, $clubname);
请注意,
->map()
优于->filter()
,因为->map()
支持一种更简单的方法来定义参数“位置”(参见参考页面中的示例2)。更新:如果可能,请参阅@pguardiario的答案
原始答案。(至少有一种选择): 不,不幸的是,phpQuery不可能。但是使用XPath可以很容易地完成 假设您必须使用以下HTML:
$html = <<<EOF
<html>
<head><title>test</title></head>
<body>
<table>
<tr>
<td>Hello</td>
<td>Hello World</td>
</tr>
</table>
</body>
</html>
EOF;
但是,如果您确实需要phpQuery解决方案,请使用以下方法:
require_once 'phpQuery/phpQuery.php';
// the search string
$needle = 'Hello';
// create phpQuery document
$document = phpQuery::newDocument($html);
// get matches as you suggested
$matches = $document->find('td:contains("' . $needle . '")');
// empty array for final results
$results = array();
// iterate through matches and check if the search string
// is the same as the node value
foreach($matches as $node) {
if($node->nodeValue === $needle) {
// put to results
$results []= $node;
}
}
// output results
foreach($results as $result) {
echo $node->nodeValue . '<br/>';
}
require_once'phpQuery/phpQuery.php';
//搜索字符串
$needle='Hello';
//创建phpQuery文档
$document=phpQuery::newDocument($html);
//按照你的建议进行匹配
$matches=$document->find('td:contains(“.$needle.”));
//最终结果的空数组
$results=array();
//遍历匹配项并检查搜索字符串
//与节点值相同
foreach($matches as$node){
如果($node->nodeValue==$pinder){
//付诸实施
$results[]=$node;
}
}
//输出结果
foreach($results作为$result){
echo$node->nodeValue.“
”;
}
可以使用过滤器完成,但它并不漂亮:
$dom->find('td')->filter(function($i, $node){return 'foo' == $node->nodeValue;});
但是,在css和xpath之间来回切换也是一样的,我知道这个问题很老了,但我已经根据
用法
谢谢这是我一直在寻找的解决方案!我对它做了一点修改,以满足我自己的进一步需求,现在它非常完美!另一个重要的注意事项是:不可能像使用jQuery filter functionAh ok那样仅使用phpQuery找到精确匹配。我不确定,但我也这么想。这就是为什么我使用了第二个foreach循环(应该大致相同)…不是我。。我真的很想知道原因,我找不到。Gijsve,文档建议我的代码应该可以工作,但我没有办法测试这些东西。你试过了吗?如果是的话,结果是什么?地图和过滤器是完全不同的概念。过滤器减少返回值,映射只是重写它。这不太正确@pguardiario.map()
当然不同于.filter()
,但与您描述的不同。正如.map()
的jQuery文档所说,“通过函数传递当前匹配集中的每个元素,生成包含返回值的新jQuery对象”。因此,.map()
是一个非常灵活的设备,它返回一个jQuery包装的数组,包含回调返回的任何内容。通过有选择地返回原始jQuery对象的DOM元素,.map()
的行为非常类似于.filter()
,但它能够应用非常特定的筛选规则,如上所述。filter可以做同样的事情。当您应该使用过滤器时,您正在使用map,因此,您在结果中得到了一堆不必要的空值。换句话说,当您需要转换元素(将它们映射为其他内容)时,请使用“映射”,当您试图减少(过滤)结果集时,请使用“过滤”。@Gijsve,请参阅我的编辑3。请试一试。我想知道它是否有效。
$dom->find('td')->filter(function($i, $node){return 'foo' == $node->nodeValue;});
<?php
// phpQuery contains function
/**
* phpQuery contains method, this will be able to locate nodes
* relative to the NEEDLE
* @param string element_pattern
* @param string needle
* @return array
*/
function contains($element_pattern, $needle) {
$needle = (string) $needle;
$needle = trim($needle);
$element_haystack_pattern = "{$element_pattern}:contains({$needle})";
$element_haystack_pattern = (string) $element_haystack_pattern;
$findResults = $this->find($element_haystack_pattern);
$possibleResults = array();
if($findResults && !empty($findResults)) {
foreach($findResults as $nodeIndex => $node) {
if($node->nodeValue !== $needle) {
continue;
}
$possibleResults[$nodeIndex] = $node;
}
}
return $possibleResults;
}
?>
<?php
$nodes = $document->contains("td.myClass", $clubname);
?>