删除
    之间的 html 换行符标签

发布于 2024-11-17 21:38:32 字数 993 浏览 4 评论 0原文

我有一个CMS系统,允许人们也使用HTML代码,但是在函数末尾提供了一个nl2br,这使得:

<ul>
<li></li>
</ul>

变成这样:

<ul><br/>
<li></li><br/>
</ul>

现在我想删除这些< ;br/> 存在于

    标记之间。

我已经发现另一个问题几乎相同,但换行符。我已将其集成到我的 CMS 中,但对于一个客户端,所有内容都已填写,因此我必须在将 \n 替换为
之后修复此问题。
的。

其他问题将此作为正则表达式提供以匹配 \

    中的 n

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

    我认为这样的事情:

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

    可以解决问题,但事实并非如此。我缺少什么?

    编辑

    我对 DOMDocument 解决方案非常开放,如果有一种方法可以使用 xpath 查询换行符,这可能会解决我的问题。

    I have a CMS system that allows people to also use HTML code, but a nl2br is provided at the end of the function, which makes this:

    <ul>
    <li></li>
    </ul>
    

    into this:

    <ul><br/>
    <li></li><br/>
    </ul>
    

    Now I want to remove these <br/>'s that exist between <ul> tags.

    I already found another question which asks almost the same, but for newlines. I've integrated this inside my CMS but for one client all the content is already filled in so I have to fix this after the \n's are replaced with <br/>'s.

    The other question provides this as a regex to match \n within <ul></ul>:

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

    I'd think something like this:

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

    Would do the trick, but it doesn't. What am I missing?

    EDIT

    I am very open for DOMDocument solutions, if there's a way to query linebreaks with xpath this would probably fix my problem.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    因为看清所以看轻 2024-11-24 21:38:32

    在您提供的示例中,
    标记被一些空格包围(至少被换行符包围),因此需要在相应的正则表达式中反映出来。

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

    在许多情况下,正则表达式并不是解析 HTML 的最佳方法(我绝对同意上面/下面的评论),但对于某些特定目的来说,它们总是足够好的。

    In the example you provided, <br> tags are surrounded by some white-space (at least by new line characters), so this needs to be reflected in the corresponding regular expression.

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

    In many cases regular expressions are NOT the best way for parsing HTML (I definitely agree with the comments above/below), but they are always good enough for some particular purposes.

    稍尽春風 2024-11-24 21:38:32

    SO 上有很多示例说明了为什么使用正则表达式解析 HTML 是一个坏主意,所以我不会在这里包含另一个示例。

    相反,请考虑使用 HTML 解析器,例如 HTMLCleanerHTML Agility Pack 来完成此任务。

    There are plenty of examples on SO that demonstrate why parsing HTML with regular expressions is a bad idea, so I won't include another one here.

    Instead, consider using an HTML parser such as HTMLCleaner or HTML Agility Pack to accomplish this task.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文