2013-02-11 38 views
0

我正在使用簡單的html dom。我有這個代碼:簡單的html dom移除元素的錯誤

<html> 
<div class="one"> 
<div class="two">this is inner text </div> 
<a href="#" class="three">this is inner anchor</a> 
This is outer test 
</div> 
</html> 

我想要取This is outer test只。 這裏是我的代碼:

$html = file_get_html(SITE_URL.'/forumlist.php'); 
$html->find('.two',0)->outertext = ""; 
$html->find('.three',0)->outertext = ""; 
$html->save(); 
echo $html->find('.one',0)->plaintext; 

和我失望..

+0

你得到了什麼錯誤? – Peon 2013-02-11 08:21:27

+0

我得到了一切..'這是內心的文字,這是內部錨點這是外部測試' – 2013-02-11 08:23:07

回答

1

至於我閱讀文檔,我不認爲你可以說出來像你想像的那麼簡單(我可能是錯行),但你可以手動str_replace刪除不需要的字符串:

$string = '<html> 
<div class="one"> 
<div class="two">this is inner text </div> 
<a href="#" class="three">this is inner anchor</a> 
This is outer test 
</div> 
</html>'; 

$html = str_get_html($string); 
echo str_replace(
     array(
      $html->find('.two',0)->plaintext, 
      $html->find('.three',0)->plaintext 
     ), 
     null, 
     $html->find('.one',0)->plaintext 
    ); 

這實際上應該做的伎倆,如果你知道HTML的結構。

+0

非常感謝你@Dainis Abols。它真的幫助了我。乾杯。 – 2013-02-11 09:01:50