![](/img/trans.png)
[英]How can i extract ul tag content and p tag content inside <dd> tag using regular expresstion in php
[英]how to extract content of all inside in p tag using php
我試圖提取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標記中的所有內容
在循環中,您在每次迭代中都重新分配$content
。 您想要的是:
foreach($html1->find('div.story-box p') as $e)
{
$content[] =$e;
}
var_dump($content);
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";
?>
如果您對打印數組感到困惑(順便說一句,只需使用print_r()
),只需串聯字符串即可:
$contents = '';
foreach($html1->find('div.story-box p') as $e) {
$contents .= $e;
// or $contents .= $e . '<br/>';
}
echo $contents;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.