Html Dom parser get first element

2019-09-01 09:43发布

问题:

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;