删除Imperavi Redactor中的空标签 [英] Remove empty tags in Imperavi Redactor

查看:127
本文介绍了删除Imperavi Redactor中的空标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Yii 2框架中使用Imperavi Redactor.

Using Imperavi Redactor with Yii 2 framework.

如果未输入任何文本,则Imperavi Redactor会生成以下标记:<p><br></p>. 对于每个换行符,该标记也会被附加.

When no text is entered, Imperavi Redactor produces this markup: <p><br></p>. For each line break this markup is appended too.

我想删除此内容,因为无法使用RequiredValidator正常验证此类内容. 我想在beforeValidate()事件中进行删除,并检查是否输入了任何文本. 如果除空标签,空格和换行符外没有其他文本,则不允许保存. 否则,内容应保存为初始状态.

I want to remove this because there is no way to normally validate such content with RequiredValidator. I want to do deletion in beforeValidate() event and check if any text is entered. If there is no text except empty tags, spaces and line breaks the saving is not allowed. Otherwise the content should be saved in initial condition.

可以在preg_replace的帮助下实现,但是我不确定这是否是Redactor生成的唯一变体.而且,即使这是唯一的变体,这种解决方案在切换选项(例如将paragraphize选项设置为false),更新或将所见即所得(例如,更改为TinyMCE或CKEditor)的情况下也不可靠.

It can be achieved with help of preg_replace, but I don't sure if it's the only variation generated by Redactor. And even it's the only variation such solution is not robust in case of switching the options (for example setting paragraphize option to false), updates or changing the WYSIWYG for example to TinyMCE or CKEditor.

例如,如果将是<p><br/></p><p>&nbsp;</p>,则正则表达式将失败. 我也想修剪空格,例如<p> <br></p>.

For example, if it will be <p><br/></p> or <p>&nbsp;</p>, regex will fail. Also I want trim spaces, for example <p> <br></p>.

Redactor中是否有执行此操作的选项? removeEmpty 选项无济于事.

Is there option in Redactor to do that? removeEmpty option does not help.

我尝试了使用 AutoFormat.RemoveEmpty 选项和结果的HTML Purifier是<p><br/></p>(对于内容<p></br></p>).也许我们需要指定自定义模式或标记列表,以便在段落中删除确切的内容.

I tried HTML Purifier with AutoFormat.RemoveEmpty option and the result was <p><br/></p> (for content <p></br></p>). Maybe we need to specify custom pattern or a list of tags what exactly should be deleted inside paragraphs.

推荐答案

找到了该解决方案:

use yii\helpers\HtmlPurifier;

$text = HtmlPurifier::process($model->text, [
    'HTML.ForbiddenElements' => ['p', 'br', '&nbsp;'],
]);

这篇关于删除Imperavi Redactor中的空标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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