dsm13698679318 2014-07-08 06:57
浏览 401
已采纳

如何使用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条回答

  • douzhuo3233 2014-07-08 07:27
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决