我目前使用以下代码在存储字符串之前对其进行清理:
ERB::Util::h(string)
当字符串已经像这样清理后,我的问题就出现了:
string = "Watching baseball `&` football"
净化后的字符串看起来像:
sanitized_string = "Watching baseball `&` football"
我可以通过替换将 < 变成 <
和 > 变成 >
来净化吗?
最佳答案
先转义,再转义:
require 'cgi'
string = "Watching baseball & football"
CGI.escapeHTML(CGI.unescapeHTML(string))
=> "Watching baseball & football"
关于Ruby Sanitize Code ... 为什么 & sanitized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1603513/