Html Dom parser get first element

房东的猫 提交于 2019-12-24 22:01:26

问题


Hi i'm using simple_html_dom php library to get contents from other website.

I have below html structure,

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

Using this

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

But i'm getting output as DSLR+D7100new

How to get only first plain text i.e, need to fetch only DSLR D7100


回答1:


You can actually get at that one with:

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



回答2:


We can use a core function to get the result what you want.

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


来源:https://stackoverflow.com/questions/22631484/html-dom-parser-get-first-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!