Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

PHP Xpath object omitted

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...