这与2012年在这里提出的问题相同,但没有得到回答:
使用正则表达式删除空段落标记<p>lt/p>("空间"上的标准str_replace不起作用)
当我在TINYMCE中按enter键时,它似乎输入了空的段落标记,如下所示:
<p> </p>
我想在将数据保存到MySQL表之前删除它们。
所以我尝试了一个简单的解决方案:
$post_content = str_replace('<p> </p>', '', $content_from_mce);
还有:
$post_content = str_replace('<p> </p>', '', $content_from_mce);
然而,它们不起作用(例如,不要替换明显为空的段落标记)。
如果我这样做:
$foo = utf8_encode($post_content);
然后检查$foo:它显示为:
<p>Â </p>
所以实际上它不是一个空的段落标记,但我不知道如何删除这些文本块。
我也尝试过这些版本(不是同时进行——我的意思是在不同的运行中…)
$post_content = str_replace('<p>Â </p>','',$post_content);
$post_content = preg_replace('~<p>'s*<'/p>~i','',$post_content);
$post_content = preg_replace('#<p> </p>#i','<p></p>', $post_content);
$post_content=str_replace("/<p> <'/p>/","",$post_content);
但它们都不起作用。
我想明白了——我正在使用HTMLPurifier来确保TinyMCE发布的内容是可以的。
$post_content通过HTMLPurifier后,它在段落标记之间包含了这个有趣的字符。
因此,如果我在通过HTMLPurifier放置$post_content之前进行替换,它可以正常工作:
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
// get contents of "post_content" field
$post_content = $_POST['post_content'];
// remove blank paragraph lines
$post_content = str_replace('<p> </p>','',$post_content);
// now put $post_content through HTMLPurifier
$post_content = $purifier->purify($post_content);