javascript - PHP 简单 HTML DOM 解析器 : Saving Dom tree

标签 javascript php jquery html dom

我正在使用PHP代码来使用PHP简单HTML DOM解析器解析网站,然后将多个div保存在htm中 文件。我遇到的问题是保存 DOM 树。目前,它是使用FILE_PUT_CONTENTS保存的,并且必须使用FILE_APPEND,以便div不会互相覆盖。但这是一个问题,因为每次运行 PHP 代码时,它都会向文件中添加我不想要的内容。我查看了几个选项,但无法理解它。我希望您能帮助我,感谢您抽出时间。

下面是我当前的PHP代码

include( 'site/simple_html_dom.php'); 
$html = file_get_html('http://roosterteeth.com/home.php');
foreach ($html->find('div.streamIndividual') as $div) 
{
file_put_contents('site/test.htm', $div, FILE_APPEND | LOCK_EX);
} 

下面是我的HTML代码,经过编辑PHP

<?php
include( 'site/simple_html_dom.php'); 
$html = file_get_html('http://roosterteeth.com/home.php');
$divContents = array();
foreach ($html->find('div.streamIndividual') as $div)
{
$divContents[] = $div->outertext;
}
file_put_contents('site/test.htm', implode(PHP_EOL, $divContents));
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
$('document').ready(function() {
$('#wrap').load('site/test.htm');
});
</script>
</head>
<body>
<div id="wrap">
</div>
</body>

下面是test.htm的链接

http://roosterteeth.t15.org/site/test.htm

最佳答案

我仍然会使用 ->outertext,但只需将内容保存到数组中,然后您可以在内爆数组上使用 file_put_contents ,从而覆盖包含所有 div 的文件:

<?php
    include( 'site/simple_html_dom.php'); 
    $html = file_get_html('http://roosterteeth.com/home.php');

    $divContents = array();

    foreach ($html->find('div.streamIndividual') as $div) 
    {
        $divContents[] = $div->outertext;
    }

    file_put_contents('site/test.htm', implode(PHP_EOL, $divContents));
?>

关于javascript - PHP 简单 HTML DOM 解析器 : Saving Dom tree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800293/

相关文章:

javascript - 窗口滚动后淡入和淡出多次发生

javascript - 函数值/名称的 Typeof 值记录为 "Undefined"。为什么?

javascript - 谷歌图表 : how do I change the percentage label color?

php - Laravel 使用外墙有什么缺点吗?

php - MySQL 和 PHP 提高性能

php - Doctrine 实体和特征。正确的方法

javascript - 在 jQuery 中显示/隐藏父 div 中的元素

javascript - 如何使 float 元素正确调整大小

javascript - 在二维数组中查找最短路径(Javascript)

javascript - 使用jquery隐藏li内的div