PHP Xpath object omitted

2020-01-20 05:01发布

问题:

I would like to get the node of an element containing a given text in the node

I tried this

$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);
    }
}

And function 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);
}

I got this error DOMXPath#4008 (1) { public $document => string(22) "(object value omitted)" }

HTML example

<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:

I've provided several possible duplicates. But in your case I think you have to correct XPath request to this (wrap $text with quotes):

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

while(list( , $node) = each($result)) {
    var_dump($node);
}