Forum: Ruby Converting escaped html to utf-8

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
12f198d3038b21ac737c05e1208c82fe?d=identicon&s=25 Chris Worrall (Guest)
on 2007-07-26 01:31
(Received via mailing list)
Hi everyone,

I've looked around online for a solution, but I'm pretty new to ruby
and programming in general, so I feel like I'm hitting a wall here.

I'm retrieving data from Hpricot that I'd like to store in UTF-8, but
I can't find a function to convert hex NCRs like:

á

Surely somebody's had to do this in the past that could point me in
the right direction? Thanks!
12f198d3038b21ac737c05e1208c82fe?d=identicon&s=25 Chris Worrall (Guest)
on 2007-07-26 18:41
(Received via mailing list)
Well, after some more googling, I found a solution. If anyone was
curious --

require 'cgi'
require 'iconv'

n = "á"
n = CGI.unescapeHTML(n)
n = Iconv.conv("UTF-8", "ISO-8859-1", n)
E0526a6bf302e77598ef142d91bdd31c?d=identicon&s=25 Daniel DeLorme (Guest)
on 2007-07-27 00:55
(Received via mailing list)
Chris Worrall wrote:
> Well, after some more googling, I found a solution. If anyone was
> curious --
>
> require 'cgi'
> require 'iconv'
>
> n = "á"
> n = CGI.unescapeHTML(n)
> n = Iconv.conv("UTF-8", "ISO-8859-1", n)

I'm surprised no one mentioned it but you could use

require "rubygems"
require "htmlentities"
puts HTMLEntities.decode_entities("Ā Ĉ Ď")
=> Ā Ĉ Ď

Daniel
This topic is locked and can not be replied to.