Encode or decode HTML entities

Posted Over 13 years ago. Visible to the public.

Use the htmlentities Show archive.org snapshot gem.

Encoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "<élan>"
coder.encode(string)               # => "&lt;élan&gt;"
coder.encode(string, :named)       # => "&lt;&eacute;lan&gt;"
coder.encode(string, :decimal)     # => "&#60;&#233;lan&#62;"
coder.encode(string, :hexadecimal) # => "&#x3c;&#xe9;lan&#x3e;"

Decoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "&eacute;lan"
coder.decode(string) # => "élan"
Henning Koch
Keywords
ruby, html, entities, unicode, utf, charset
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2010-12-07 09:36)