【问题标题】:Html Dom parser get first elementHtml Dom解析器获取第一个元素
【发布时间】:2014-05-03 02:42:56
【问题描述】:

您好,我正在使用simple_html_dom php 库从其他网站获取内容。

我有下面的html结构,

<h1 class="nik_product_title" style="color: #000;">
  DSLR D7100
  <span class="new_big_parent">
    <span class="new_big_child">
      <span class="new_big_child1">new</span>
    </span>
  </span>
</h1>

使用这个

@$html->find ( 'div[class=nik_block_product_main_info_component_inner] h1',0)->plaintext;

但我得到的输出是DSLR+D7100new

如何只获取第一个纯文本,即只需要获取DSLR D7100

【问题讨论】:

    标签: php html-parsing simple-html-dom


    【解决方案1】:

    您实际上可以通过以下方式获得:

    $html->find('h1 text', 0);
    

    【讨论】:

    • 这么简单!!没有意识到这一点。谢谢你 pguardiario。
    • 感谢@pguardiario 这个非常简单的答案
    • @pguardiario 你对this有什么想法吗
    【解决方案2】:

    我们可以使用一个核心函数来得到你想要的结果。

    $html = str_get_html('<h1 class="nik_product_title" style="color: #000;">
    DSLR D7100
    <span class="new_big_parent">
    <span class="new_big_child">
      <span class="new_big_child1">new</span>
    </span>
    </span>
    </h1>');
    
    $last_one =$html->find('h1.nik_product_title',0)->children (0)->plaintext;
    
    $whole =$html->find('h1.nik_product_title',0)->plaintext;
    
    $result = str_replace($last_one,"",$whole);
    echo $result;
    

    【讨论】:

    • 谢谢古纳。成功了!!
    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多