CakePHP 有一个名为 h
的全局函数。这是 htmlspecialchars
的一种便捷方法。 CakePHP 还有一个名为 Sanitize
的实用程序,它有一个名为 html
的方法。以下是其部分描述:
This method prepares user-submitted data for display inside HTML. This is especially useful if you don’t want users to be able to break your layouts or insert images or scripts inside of your HTML pages.
什么时候应该使用它们?一个比另一个更好吗?
最佳答案
Sanitize::html()
更加通用:它允许您完全剥离 HTML(通过 remove
选项),并允许您指定它处理引用的方式。
查看源代码:
h()
:http://api.cakephp.org/2.3/source-function-h.html#160-199
清理::html()
:http://api.cakephp.org/2.3/source-class-Sanitize.html#83-122
编辑:
h()
:调用htmlspecialchars()
Sanitize::html()
:调用htmlentities()
有关差异的讨论,请参阅:htmlentities() vs. htmlspecialchars()
关于CakePHP:h() 与 Sanitize::html(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17159548/