ds3464 2009-08-14 10:24
浏览 56
已采纳

从html标记之间删除换行符

I'm making a form (html & php) which is part of an admin section used to edit content for a website. I want to allow users to include some basic html. This works fine. I want to retain line breaks. This also works. My problem is that when someone writes something like this:

<ul>
<li>item one</li>
<li>item two</li>
</ul>

the line breaks between the lines of code are retained and turned into BRs when written out. This means that there's double spacing between each LI element. Now this can be fixed by writing the whole list section on one line but a) that makes it confusing to read and b) it's hard enough teaching people to use the codes let alone explaining extraineous line breaks.

What I want is some way to strip all /n out but ONLY between UL and /UL tags.

  • 写回答

4条回答 默认 最新

  • douxie1894 2009-08-14 10:41
    关注

    This regular expression removes all linebreaks/whitespaces between <ul> and </ul> that are not part of the text between <li> and </li>

    /(?<=<ul>|<\/li>)\s*?(?=<\/ul>|<li>)/is
    

    php example:

     $output = preg_replace('/(?<=<ul>|<\/li>)\s*?(?=<\/ul>|<li>)/is', '', $input);
    

    input:

    <ul>
    <li>item one</li>
    <li>item two</li>
    </ul>
    

    output:

    <ul><li>item one</li><li>item two</li></ul>
    

    EDIT: fixed

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?