I am trying to decode some HTML entities, such as '<'
becoming '<'
.
I have an old gem (html_helpers) but it seems to have been abandoned twice.
Any recommendations? I will need to use it in a model.
HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as < and > for HTTP transmission.
Load the data to HTML–encode from a file, then press the 'Encode' button: Browse: Alternatively, type or paste in the text you want to HTML–encode, then press the 'Encode' button.
Wikipedia has a good expalanation of character encodings and how some characters should be represented in HTML. Load the HTML data to decode from a file, then press the 'Decode' button: Browse: Alternatively, type or paste in the text you want to HTML–decode, then press the 'Decode' button.
HTML DecodeThe encoded characters are converted back to their original form in the decoding process. It decodes a string that contains HTML numeric character references and returns the decoded string. You can also choose to convert HTML code into JavaScript string.
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>' %>
HTMLEntities can do it:
: jmglov@laurana; sudo gem install htmlentities Successfully installed htmlentities-4.2.4 : jmglov@laurana; irb irb(main):001:0> require 'htmlentities' => [] irb(main):002:0> HTMLEntities.new.decode "¡I'm highly annoyed with character references!" => "¡I'm highly annoyed with character references!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With