Mohamed El Mahallawy
Mohamed El Mahallawy

Reputation: 13842

Ruby string force encoding

How can I force encode this: Al-F\u0026#257;ti\u0026#293;ah to Al-Fātiĥah

I tried .encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "") and force_encoding("UTF-8") with no luck

Upvotes: 0

Views: 250

Answers (1)

labocho
labocho

Reputation: 231

That text seems to include HTML or XML entities.

Try

require "cgi/util"
CGI.unescapeHTML("Al-F\u0026#257;ti\u0026#293;ah")

or

# gem install nokogiri
require "nokogiri"
Nokogiri::XML.fragment("Al-F\u0026#257;ti\u0026#293;ah").text

See: Converting escaped XML entities back into UTF-8

Upvotes: 1

Related Questions