duanchensou8685 2013-04-21 18:54
浏览 10
已采纳

如何显示前两个段落? 然后是剩下的段落? - PHP

I have 4 paragraphs of text in one string. Each paragraph is surrounded with <p></p>.

  1. My first goal is to output the first 2 paragraphs.
  2. My second goal it to output the remaining paragraphs somewhere else on the page. I could sometimes be dealing with strings containing more than 4 paragraphs.

I've searched on the web for anything already out there. There's quite a bit about displaying just the first paragraph, but nothing I could find about displaying paragraphs 1-2 and then the remaining paragraphs. Can anyone help here?

Not sure which to use if any, substr, strpos, etc.....?

EDIT - thanks for your answers, to clarify, the paragraphs don't contain HTML at the moment, but yes I will need the option to have HTML within each paragraph.

  • 写回答

2条回答 默认 最新

  • dongsi2317 2013-04-21 19:11
    关注

    Use DOMDocument

    Initialize with:

    $dom = new DOMDocument;
    $dom->loadHTML($myString);
    $p = $dom->getElementsByTagName('p');
    

    If each can contains other HTML elements(or not), create a function:

    function getInner(DOMElement $node) {
        $tmp = "";
        foreach($node->childNodes as $c) {
            $tmp .= $c->ownerDocument->saveXML($c);
        }
        return $tmp;
    }
    

    and then use that function when needing the paragraph like so:

    $p1 = getInner($p->item(0));
    

    You can read more about DOMDocument here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题