3

Google Book Search Give Output Something like this

Introducing Little Simon's new Baby Snoopy line, featuring Charles Schulz'scharacters as babies. 

How do i convert this into normal readable string ?

I tried URI.unescape and searched it online , but couldn't find anything. I am a newbie in Ruby on Rails.

3 Answers 3

4

Maybe you need to tell Rails that it's HTML safe.....

controller

@str = get_str_from_google_book_search(ie.your.string)

view

<%= @str.html_safe %>
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to escape that string. The following works just fine.

irb(main):001:0> s="Introducing Little Simon's new Baby Snoopy line, featuring Charles Schulz'scharacters as babies."
=> "Introducing Little Simon's new Baby Snoopy line, featuring Charles Schulz'scharacters as babies."
irb(main):002:0> s
=> "Introducing Little Simon's new Baby Snoopy line, featuring Charles Schulz'scharacters as babies."

If you had a character you needed to escape, such as ", you could escape it with a \ as so:

irb(main):008:0> s = "a\"b"
=> "a\"b"
irb(main):009:0> puts s
a"b
=> nil

2 Comments

I think you misunderstood my question I am getting this data from somewhere else , I am not adding this data by hand.
Right, I believe the exact issue here is the numeric character references; the general issue is any HTML markup. I've edited the question so you can see the reference.
0

The string is already HTML, and you are working in Rails, so I'm not sure that you really need to boil it down further. While the HTML numeric character references in your example string do have both ASCII and Unicode equivalents, anything more complicated like <p> will cause trouble.

However, for your example string, there is a way.

You can use an HTML parser to parse the string as a fragment.

require 'nokogiri'
s1 = 'How&#39;s life?'
s2 = Nokogiri::HTML.fragment(s1).to_s
puts s2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.