我试图在div类中提取html的所有p标签的内容,并尝试将整个流插入一个变量数组
这是html代码示例
<html>
...
<div class=news-box>
<h2>Heading</h2>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>yuoyuouoyuoyuyu oyuiouioyuioyuyiouyoiy youyoiyuioyuioyuyoiuyiuyiyuioyu yuyiu</p>
</div>
...
</html>
我想提取p中的所有内容并尝试将整个流插入变量以插入数据库,我在下面使用这些php代码
<?php
include('simple_html_dom.php');
$url = 'http://www.example.com';
$html1=file_get_html($url);
$content=array();
foreach($html1->find('div.story-box p') as $e)
{
$content=$e;
}
echo $content;
?>
为什么它只显示最后一行...想要在一个变量$e
中分配后显示p标签中的所有内容
答案 0 :(得分:0)
在循环中,您在每次迭代时重新分配$content
。你想要的是:
foreach($html1->find('div.story-box p') as $e)
{
$content[] =$e;
}
var_dump($content);
答案 1 :(得分:0)
temp1.html
<html>
<head><title></title></head>
<body>
<div id="text">
<h2>Heading</h2>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
<p>yuoyuouoyuoyuyu oyuiouioyuioyuyiouyoiy youyoiyuioyuioyuyoiuyiuyiyuioyu yuyiu</p>
</div>
</body>
</html
temp1.php
<?php
include("simplehtmldom_1_5/simple_html_dom.php");
$url = 'http://localhost/temp1.html';
$html=file_get_html($url);
$content=array();
$a="";
foreach($html->find('div[id=text] p') as $e)
{
$a .=$e->plaintext." ";
// echo $e->plaintext."<br>";
}
echo "$a";
?>
答案 2 :(得分:0)
如果您对打印数组感到困惑(顺便使用print_r()
),只需连接字符串:
$contents = '';
foreach($html1->find('div.story-box p') as $e) {
$contents .= $e;
// or $contents .= $e . '<br/>';
}
echo $contents;