使用DOM提取内容betwin div标签,但同时记录其他html标签

时间:2015-09-04 15:45:16

标签: php html dom

我想从带有DOM的html文件中提取标签内容。

源文件:

<html>
<body>
some html code
..........
<div id="text"> 
<p>some title</p> <br>
<p>some text</p> <br>
<img src="../images2/somegif.gif">
<div>
..........
</body>
</html>

我的代码:

$file = 'somefile.html';
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file($file);
$text = $html->getElementById('text');

echo $text;

结果是:

<div id="text"> 
<p>some title</p> <br>
<p>some text</p> <br>
<img src="../images2/somegif.gif">
<div>

我想要的只是div标签内的数据,但保留所有其他HTML元素,如:

<p>some title</p> <br>
<p>some text</p> <br>
<img src="../images2/somegif.gif">

我该怎么做? ...我需要稍后将数据发送到mysql数据库。谢谢。

1 个答案:

答案 0 :(得分:0)

$text1= $html->getElementById('text');
$text= $text1->innertext;
echo $text;

我只是更改了var名称...... LOL