简单的html dom删除元素时出错


Error on simple html dom removing elements

我使用的是简单的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;

我很失望。。

就我阅读的文档而言,我不认为你能像你想象的那样容易地把它说出来(当然我可能错了),但你可以用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的结构,这应该真的很管用。