使用正则表达式删除空的段落标记

(“空间”上的标准str_replace不起作用)

使用正则表达式删除空的段落标记<p> </ p>(“空间”上的标准str_replace不起作用)

问题描述:

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

当您按两次输入时,TinyMCE会创建空的段落标记。 像: p>

 &LT; p为H.  &lt; / p&gt; 
  code>  pre> 
 
 

&lt; p&gt; SPACE&lt; / p&gt; code> p>

在FireBug中,它将此空间称为“&amp; nbsp;” code>,但html代码/数据库后端只显示一个空格。 当我执行“str_replace('&lt; p&gt;&lt; / p&gt;'.....” code>时,它找不到块...基本上我认为“空间”不是一个标准 空间和某种borked编码空间。有没有我可以运行的正则表达式会删除这个标签?我已经坚持了几个小时...甚至像 p>

正则表达式('&lt; p&gt;少于三个字符&lt; / p&gt;'...) code> p>

可能会起作用 p>

谢谢 p> div>

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.

Try this

$string="a bunch of text with <p> </p> in it";

$string=str_replace("/<p> <\/p>/","",$string);

Note a couple things: the forward slashes before and after the string to match, as well as the escaping backslash before the forward slash in the second paragraph tag.

$text = preg_replace('#<p>&nbsp;</p>#i','<p></p>', $text);

worked for me, as the variable contains the actual string "&nbsp;" and not the non-breaking space unicode character. Thus neither #<p>.</p>#i worked nor copying the non-breaking-space character from character map.

The answers above won't work if <p> tag has any inline attributes, such as <p style="font-weight:bold">.

Here is a regex to catch it:

#<p[^>]*>(\s|&nbsp;|</?\s?br\s?/?>)*</?p>#

None of the given answers were working for me, but here's what did:

$str = str_replace('&lt;p&gt;&nbsp;&lt;/p&gt;', '', $str);

Definitely not the most correct way to do things. But if you're working with (against) TinyMCE, specifically inside of SuiteCRM, this should help.