Vinoth Pandiyan
Vinoth Pandiyan

Reputation: 241

Html Dom parser get first element

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

Upvotes: 0

Views: 1543

Answers (2)

pguardiario
pguardiario

Reputation: 54984

You can actually get at that one with:

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

Upvotes: 3

Gunaseelan
Gunaseelan

Reputation: 2542

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;

Upvotes: 1

Related Questions