views:

1778

answers:

1

I am trying to decode some HTML entities ('&lt;' => '<'). I have found an old gem (http://github.com/tma/html_helpers/) but it seems to have been abandoned twice.

Any recommendations? (will need to use it in a model)

+11  A: 

To encode the characters, you can use CGI.escapeHTML

string = CGI.escapeHTML('test "escaping" <characters>')

To decode them, there is the CGI.unescapeHTML

CGI.unescapeHTML("test &quot;unescaping&quot; &lt;characters&gt;")

Of course, you need, before that, 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>' %>
Damien MATHIEU
I tried this approach first but it does not turn entities like " " into " ".I guess I should specify that I get the html from a bunch of different sites and need to save it as plain text in the database.
vrinek