删除html标签之间的换行符 [英] Remove line breaks from between html tags

查看:402
本文介绍了删除html标签之间的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个表单(html和php),它是用于编辑网站内容的管理部分的一部分。我想让用户包含一些基本的html。这工作正常。我想保留换行符。这也适用。我的问题是,当有人写这样的东西:

 < ul> 
< li>第1项< / li>
< li>第二项< / li>
< / ul>

代码行之间的换行符被保留,并在写出时转换为BR。这意味着每个LI元素之间有两倍的间距。现在可以通过在一行上写整个列表部分来解决这个问题,但是a)这会让读者感到困惑,并且b)很难教导人们如何使用代码,更不用说解释突然出现的换行符。



我想要的是某种方法可以去掉所有/ n,但只能在UL和/ UL标签之间。

解决方案

这个正则表达式删除了< ul> < / ul> 之间的所有换行符/空格不是< li> < / li>


之间文字的一部分

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

php示例:
$ b $ $ output = preg_replace('/(?<=

    \\ / ul> |< li>)/是','',$ input);

    输入:

     < UL> 
    < li>第1项< / li>
    < li>第二项< / li>
    < / ul>

    输出:

     < ul>< li>第1项< / li>< li>第2项< / li>< / ul> 

    编辑:固定 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.

    解决方案

    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

    这篇关于删除html标签之间的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆