2014-03-25 51 views
0

嗨,我使用simple_html_dom php庫從其他網站獲取內容。Html Dom解析器獲取第一個元素

我有以下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

回答

2

其實你可以得到的一個具有:

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

太簡單了!沒有意識到這一點。謝謝pguardiario。 – Vind

+0

感謝@pguardiario這個非常簡單的答案 – Gunaseelan

+0

@pguardiario您對[this]有任何想法嗎?(http://stackoverflow.com/questions/22669373/php-simple-html-dom-parser-cant-get-content-上分頁) – Vind

1

我們可以使用核心函數來獲得結果你想要什麼。

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

感謝古納。有效 !! – Vind