簡體   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