繁体   English   中英

我可以使用phpQuery找到完全匹配的内容吗?

[英]Can I find exact match using phpQuery?

我使用phpQuery编写了一个脚本。 该脚本查找包含特定字符串的td:

$match = $dom->find('td:contains("'.$clubname.'")');

到现在为止效果很好。 因为一个俱乐部名称是例如奥地利Lustenau,第二个俱乐部名称是Lustenau。 它将选择两个俱乐部,但仅应选择Lustenau(第二个结果),因此我需要找到一个包含完全匹配项的td。

我知道phpQuery使用的是jQuery选择器,但不是全部。 有没有一种方法可以使用phpQuery查找完全匹配?

更新:有可能,请参阅@ 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;

使用以下代码查找与DOMXPath完全匹配的内容:

// create empty document 
$document = new DOMDocument();

// load html
$document->loadHTML($html);

// create xpath selector
$selector = new DOMXPath($document);

// selects all td node which's content is 'Hello'
$results = $selector->query('//td[text()="Hello"]');

// output the results 
foreach($results as $node) {
    $node->nodeValue . PHP_EOL;
}

但是,如果您确实需要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/>';
}

我没有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

这是jQuery版本演示

如果phpQuery做到了其文档中所说的内容 ,那么(正确翻译为javascript)它应该以相同的方式匹配所需的元素。

编辑3

在对phpQuery回调系统有更多了解之后,下面的代码更容易实现工作:

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)。

可以使用过滤器来完成,但这并不漂亮:

$dom->find('td')->filter(function($i, $node){return 'foo' == $node->nodeValue;});

但是然后,两者都不会在css和xpath之间来回切换

我知道这个问题很旧,但是我已经根据hek2mgl的答案写了一个函数

<?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);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM