[英]PHP Simple HTML DOM Parser: Saving Dom tree
I am using PHP code to parse a website using PHP Simple HTML DOM Parser and then save multiple div's in a htm file. 我使用PHP代码使用PHP Simple HTML DOM Parser解析网站,然后在htm文件中保存多个div 。 The problem I am having is saving the DOM Tree .
我遇到的问题是保存DOM树 。 As of the moment the it is being saved using FILE_PUT_CONTENTS and has to use FILE_APPEND so that the div's wont over write each other.
由于一时的它正在使用FILE_PUT_CONTENTS保存,并有权使用FILE_APPEND使div的不会写上对方。 But this is a problem as each time the PHP code is run it adds more to the file which I don't want.
但这是一个问题,因为每次运行PHP代码时,它都会向我不想要的文件添加更多内容。 I looked at a few options but couldn't make sense of it.
我看了几个选项,但无法理解它。 I hope you can help me and thank you for your time.
我希望你能帮助我,谢谢你的时间。
Below is my current PHP code 下面是我目前的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);
}
Below is my HTML code with edited PHP 下面是我编辑PHP的 HTML代码
<?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>
Below is the link for test.htm 以下是test.htm的链接
http://roosterteeth.t15.org/site/test.htm http://roosterteeth.t15.org/site/test.htm
I'd still use ->outertext
, but simply save the content to an array, and then you can use file_put_contents
on the imploded array, and hence overwrite the file with all of the divs: 我仍然使用
->outertext
,但只是将内容保存到数组,然后你可以file_put_contents
爆数组上使用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));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.