使用正则表达式删除空段落标签 <p></p>(“空间"上的标准 str_replace 不起作用) [英] Using regex to remove empty paragraph tags <p> </p> (standard str_replace on "space" not working)

查看:21
本文介绍了使用正则表达式删除空段落标签 <p></p>(“空间"上的标准 str_replace 不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您按两次 Enter 键时,TinyMCE 会创建空的段落标记.喜欢:

</p>

这是

SPACE

在 FireBug 中,它将此空间称为 "&nbsp;" 但 html 代码/DB 后端只显示一个空间.当我做 "str_replace('<p> </p>'....." 它没有找到块......基本上我认为空间"在某种程度上不是标准空间和某种无聊的编码空间.有没有我可以运行的正则表达式来删除这个标签?我已经坚持了几个小时......甚至像

这样的东西

regex('

少于三个字符

'...)

可能会起作用

谢谢

解决方案

我会使用:

$str = preg_replace('~

\s*<\/p>~i','',$str);

其中 \s 表示任何类型的空格(制表符、空格等),* 表示此(空格)出现 0 次或多次.所以

,

</p><p>{此处多空格}</p> 将全部替换为空字符串.额外的 i 标志是为了不区分大小写,以防

的可能是

的.

TinyMCE creates empty paragraph tags when you hit enter twice. like:

<p> </p>

Which is <p>SPACE</p>

In FireBug it calls this space a "&nbsp;" but the html code/DB backend just shows a space. When I do "str_replace('<p> </p>'....." it doesnt find the block... basically I think the "space" is somehow not a standard space and some sort of borked encoded space. Is there a regex I can run that will remove this tag? I've been stuck on this for hours... or even something like

regex('<p>LESS THAN THREE CHARS</p>'...)

would probably work

Thank you

解决方案

I would use:

$str = preg_replace('~<p>\s*<\/p>~i','',$str);

where \s signifies a white space of any kind (tab, space, etc.) and * indicates 0 or more occurence of this (space). So <p></p>, <p> </p>, <p>{multiple spaces here}</p> will all be replaced by an empty string. The additional i flag is for case-insensitivity, just in case <p>'s might instead be <P>'s.

这篇关于使用正则表达式删除空段落标签 &lt;p&gt;&lt;/p&gt;(“空间"上的标准 str_replace 不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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