Php 如何在Symfony 1.2中允许表单字段中使用某些HTML标记

Php 如何在Symfony 1.2中允许表单字段中使用某些HTML标记,php,forms,symfony1,Php,Forms,Symfony1,我在和Symfony玩,遇到了一个路障 我创建了一个模型“CmsPage”,它有一个名为“content”的字段,存储为clob(我认为这是特定于条令的)。当我创建应用程序时,我将“-escaping strategy=on”设置为“因此,如果我在编辑使用html实体或类似内容编码的CmsPage时输入任何html。我想在这个领域允许html,但快速谷歌搜索并没有多大帮助。也许我在寻找错误的术语 我希望禁用此字段的字符转义,并且可能只允许少量html标记选择。在Symfony中执行此操作的正确方

我在和Symfony玩,遇到了一个路障

我创建了一个模型“CmsPage”,它有一个名为“content”的字段,存储为clob(我认为这是特定于条令的)。当我创建应用程序时,我将“-escaping strategy=on”设置为“因此,如果我在编辑使用html实体或类似内容编码的CmsPage时输入任何html。我想在这个领域允许html,但快速谷歌搜索并没有多大帮助。也许我在寻找错误的术语

我希望禁用此字段的字符转义,并且可能只允许少量html标记选择。在Symfony中执行此操作的正确方法是什么?

来自

每个模板都可以访问$sf_数据变量,该变量是引用所有转义变量的容器对象。 [跳过] $sf_数据还允许您访问未扫描的或原始的数据。如果您信任此变量,则当变量存储要由浏览器解释的HTML代码时,这非常有用。当需要输出原始数据时,调用getRaw()方法

echo$sf_data->getRaw('test'); =>警报(document.cookie)每次需要将包含HTML的变量真正解释为HTML时,您都必须访问原始数据。现在您可以理解为什么默认布局使用$sf\u data->getRaw('sf\u content')来包含模板,而不是更简单的$sf\u内容,该内容在激活输出转义时会中断。

From

每个模板都可以访问$sf_数据变量,该变量是引用所有转义变量的容器对象。 [跳过] $sf_数据还允许您访问未扫描的或原始的数据。如果您信任此变量,则当变量存储要由浏览器解释的HTML代码时,这非常有用。当需要输出原始数据时,调用getRaw()方法

echo$sf_data->getRaw('test'); =>警报(document.cookie)每次需要将包含HTML的变量真正解释为HTML时,您都必须访问原始数据。现在,您可以理解为什么默认布局使用$sf_data->getRaw(“sf_内容”)来包含模板,而不是更简单的$sf_内容,该内容在激活输出转义时会中断。

您可以使用它来满足您的需要

下面是htmlpurifier的小配置。这些规则与TinyMce编辑器完美匹配

$purifier = new HTMLPurifier();
$purfier_config = HTMLPurifier_Config::createDefault();
$purfier_config->set('HTML.DefinitionID', 'User Content Filter');
$purfier_config->set('HTML.DefinitionRev', 1);
// these are allowed html tags, means white list
$purfier_config->set('HTML.Allowed', 'a,strong,em,p,span,img,li,ul,ol,sup,sub,small,big,code,blockquote,h1,h2,h3,h4,h5');
// these are allowed html attributes, coool!
$purfier_config->set('HTML.AllowedAttributes', 'a.href,a.title,span.style,span.class,span.id,p.style,img.src,img.style,img.alt,img.title,img.width,img.height');
// auto link given url string
$purfier_config->set('AutoFormat.Linkify', true);
// auto format \r\n lines
$purfier_config->set('AutoFormat.AutoParagraph', true);
// clean empty tags
$purfier_config->set('AutoFormat.RemoveEmpty', true);
// cache dir, just for symfony of course, you can change to another path
$purfier_config->set('Cache.SerializerPath', sfConfig::get('sf_cache_dir'));
// translation type, 
$purfier_config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
// allow youtube videos
$purfier_config->set('Filter.YouTube', true);
$purfier_config->set('HTML.TidyLevel', 'heavy');
// now clean your data
$clean_nice_html_data = $purifier->purify($user_input_data, $purfier_config);
现在,您可以使用html标记将数据插入数据库,而无需转义数据,因为htmlpurifier可以为您清除讨厌的、危险的数据,并且只接受允许的标记和属性

我希望它能帮上忙。

您可以使用它,它是满足您需求的绝佳工具

下面是htmlpurifier的小配置。这些规则与TinyMce编辑器完美匹配

$purifier = new HTMLPurifier();
$purfier_config = HTMLPurifier_Config::createDefault();
$purfier_config->set('HTML.DefinitionID', 'User Content Filter');
$purfier_config->set('HTML.DefinitionRev', 1);
// these are allowed html tags, means white list
$purfier_config->set('HTML.Allowed', 'a,strong,em,p,span,img,li,ul,ol,sup,sub,small,big,code,blockquote,h1,h2,h3,h4,h5');
// these are allowed html attributes, coool!
$purfier_config->set('HTML.AllowedAttributes', 'a.href,a.title,span.style,span.class,span.id,p.style,img.src,img.style,img.alt,img.title,img.width,img.height');
// auto link given url string
$purfier_config->set('AutoFormat.Linkify', true);
// auto format \r\n lines
$purfier_config->set('AutoFormat.AutoParagraph', true);
// clean empty tags
$purfier_config->set('AutoFormat.RemoveEmpty', true);
// cache dir, just for symfony of course, you can change to another path
$purfier_config->set('Cache.SerializerPath', sfConfig::get('sf_cache_dir'));
// translation type, 
$purfier_config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
// allow youtube videos
$purfier_config->set('Filter.YouTube', true);
$purfier_config->set('HTML.TidyLevel', 'heavy');
// now clean your data
$clean_nice_html_data = $purifier->purify($user_input_data, $purfier_config);
现在,您可以使用html标记将数据插入数据库,而无需转义数据,因为htmlpurifier可以为您清除讨厌的、危险的数据,并且只接受允许的标记和属性

我希望有帮助