省略了PHP Xpath对象

时间:2017-05-15 10:38:46

标签: php xpath simple-html-dom

我想在节点

中获取包含给定文本的元素的节点

我试过这个

$template = file_get_contents($file);
// search for the query get_query_var( 's' )
if (stripos($template, get_query_var( 's' )) !== false) {
    $html_content = str_get_html($template); // using simple_html_dom_parser
    if($html_content->find('div.row', 0)){
        $content = $html_content->find('div.row', 0)->innertext;
        print findtext($content, $words);
    }
}

功能findtext

function findtext($text,$word){
    libxml_use_internal_errors(true);
    //var_dump($text);
    $dom = new DomDocument();
    /* Load the HTML */
    $dom->loadHTML($text);
    /* Create a new XPath object */
    $xp = new DomXPath($dom);
    $result = $xp->query("//*[text()[contains(., '$text')]]");
    var_dump($result);
}

我收到此错误DOMXPath#4008(1){public $ document => string(22)"(省略对象值)" }

HTML示例

<p class="smallerText blackB">

<b class="">Le COMIDENT est</b> également et particulièrement, 
le <b class="">partenaire de l’Association Dentaire Française (ADF)</b>
avec qui il co-organise, en étroite collaboration chaque année, 
l’Exposition du Congrès annuel au Palais des Congrès, qui regroupe
environ 400 exposants répartis sur 4 niveaux.
</p>

1 个答案:

答案 0 :(得分:0)

我提供了几个可能的副本。但在你的情况下,我认为你必须纠正XPath请求(用引号括起$text):

$result = $xp->query("//*[text()[contains(., '$text')]]");

while(list( , $node) = each($result)) {
    var_dump($node);
}
相关问题