To encode the characters, you can use CGI.escapeHTML
:
string = CGI.escapeHTML('test "escaping" <characters>')
To decode them, there is CGI.unescapeHTML
:
CGI.unescapeHTML("test "unescaping" <characters>")
Of course, before that you need to include the CGI library:
require 'cgi'
And if you're in Rails, you don't need to use CGI to encode the string. There's the h
method.
<%= h 'escaping <html>' %>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…