dongyin8009 2017-05-15 10:38
浏览 29

省略了PHP Xpath对象

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条回答 默认 最新

  • dongzhuo1930 2017-05-15 10:44
    关注

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号