省略了 PHP Xpath 对象 [英] PHP Xpath object omitted

查看:23
本文介绍了省略了 PHP Xpath 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我试过了

$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

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

我收到了这个错误 DOMXPath#4008 (1) { public $document => string(22) "(object value省略)" }

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

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>

推荐答案

我提供了几个可能的重复项.但在你的情况下,我认为你必须纠正 XPath 请求(用引号包裹 $text):

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

这篇关于省略了 PHP Xpath 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆