user3688241
user3688241

Reputation: 3145

Unable to decode double encoded special characters in rails

I'm getting data from an api where special characters are are getting double encoded. By that I mean ’ is encoded as ’. I know how to decode but I am unable to double decode. I've tried raw and html_safe but neither will decode it past ’, even if i double up i.e. raw raw or .html_safe.html_safe. How can I completely decode these characters?

Upvotes: 0

Views: 383

Answers (2)

Datpmt
Datpmt

Reputation: 1

I have used to have the same problem. my workaround for all HTML problems is as follows:

def format_html_sentence(sentence)
    Nokogiri::HTML.parse(sentence.gsub(/(\\r|\\n)/, '')).text
end

Upvotes: 0

SteveTurczyn
SteveTurczyn

Reputation: 36860

This works...

require 'rubygems'
require 'nokogiri'

my_string = "This is Sam’s bicycle"

decoded_string = Nokogiri::HTML(my_string.gsub('&','&')).text 

puts decoded_string
=> => "This is Sam's bicycle"

Upvotes: 1

Related Questions