【问题标题】:Removing "blank" paragraph tags from TinyMCE从 TinyMCE 中删除“空白”段落标签
【发布时间】:2016-05-17 15:23:02
【问题描述】:

这与 2012 年在这里提出的问题相同,但没有得到回答:

Using regex to remove empty paragraph tags <p> </p> (standard str_replace on "space" not working)

当我在 TINYMCE 中按 enter 时,它似乎输入了这样的空段落标签:

<p> </p>

我想在将数据保存到 MySQL 表之前删除它们。

所以我尝试了一个简单的修复:

$post_content = str_replace('<p> </p>', '', $content_from_mce);

还有:

$post_content = str_replace('<p>&nbsp;</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>&nbsp;</p>#i','<p></p>', $post_content);
$post_content=str_replace("/<p> <\/p>/","",$post_content);

但它们都不起作用。

【问题讨论】:

    标签: php tinymce tinymce-4


    【解决方案1】:

    我想通了 - 我正在使用 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>&nbsp;</p>','',$post_content);
    
    // now put $post_content through HTMLPurifier 
    $post_content = $purifier->purify($post_content);
    

    【讨论】: