2014-03-25 129 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