如何使用php提取p标签内的所有内容

i am trying to extract the contents of all p tags of html inside div class and trying to insert whole stream into one variable array

this are html code example

<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>

i want to extract all content in p and trying to insert whole stream into variable for to insert in database, i am using below these php codes

<?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;
?>

why it is showing only last line... want to show all contents in p tag after assigning in one variable not $e

3个回答

If you're confused at printing arrays (just use print_r() by the way), just concatenate the strings:

$contents = '';
foreach($html1->find('div.story-box p') as $e) {
    $contents .= $e;
    // or $contents .= $e . '<br/>';
}

echo $contents;
douzhuanfen5923
douzhuanfen5923 很高兴@RIco工作
6 年多之前 回复
dongxian7489
dongxian7489 谢谢你@ MarioD'Boro我得到了我想要的东西。
6 年多之前 回复

In your loop, you re-assign $content on every iteration. What you want is:

foreach($html1->find('div.story-box p') as $e)
{
    $content[] =$e;
}
var_dump($content);
duanbichenhe1968
duanbichenhe1968 使用print_r()进行数组打印
6 年多之前 回复
douxueke5653
douxueke5653 你会注意到我使用了var_dump($ content);
6 年多之前 回复
dongyuying1507
dongyuying1507 谢谢你的回复,我是php的新手,在php中学习新东西。 如何回显出来在浏览器中显示。 我尝试这个echo $内容,但它显示错误“注意:数组到字符串转换在C:\ xampp \ htdocs \ test1.php第23行数组”。 你能告诉我如何在循环后在浏览器中回显。
6 年多之前 回复

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";
   ?>
duanjianl183188
duanjianl183188 检查已编辑的答案,如果有帮助请告诉我们。
6 年多之前 回复
doupeng6890
doupeng6890 这意味着它不会从p标签中检索所有值。
6 年多之前 回复
douba2705
douba2705 它遇到与OP相同的问题,$ content只是最后一个元素。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐