cam*_*ase 0 php forms symfony1
我和Symfony一起玩,遇到过路障.
我创建了一个模型"CmsPage",它有一个名为"content"的字段,存储为clob(这是我所相信的特定的学说).当我创建应用程序时,我设置"--escaping-strategy = on",所以如果我在编辑使用html实体编码的CmsPage时输入任何html,或者沿着这些行编写某些内容.我想在这个领域允许HTML,快速的谷歌搜索没有多大帮助.也许我正在寻找错误的条款.
任何我想禁用该字段的字符转义,并且可能只允许少量html标记.在Symfony中执行此操作的正确方法是什么?
您可以使用http://htmlpurifier.org/这是满足您需求的绝佳工具.
这是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);
Run Code Online (Sandbox Code Playgroud)
现在您可以使用html标签将数据插入数据库,并且您不需要转义数据,因为,htmlpurifier会为您清除令人讨厌的危险数据,并且只接受您允许的标签和属性.
我希望它有所帮助.