1

Specifically, suppose I start with the string

  string ="hello \'i am \' me"

And then I textilize the string to get an output:

  textilize(string) => "<p>hello &#8217;i am &#8217; me</p>"

And then, because I want to get a count of the characters visible to a person, I wish to strip the html tags, and reverse the special characters ’ to the simple apostrophe they are (ie ' or \'). Strip_tags is easy enough:

 strip_tags(textilize(string)) => "hello &#8217;i am &#8217; me"

But I don't know how to reverse the special characters. If it helps, I know the method html_escape would have turned some characters like angle brackets into the decimal form:

 html_escape(">") => "&gt;"

But I don't know of a rails method to reverse this. Anyone have suggestions on either a custom method or a rails method to accomplish this task?

jay
  • 12,066
  • 16
  • 64
  • 103
  • possible duplicate of [How do I encode/decode HTML entities in Ruby?](http://stackoverflow.com/questions/1600526/how-do-i-encode-decode-html-entities-in-ruby) – Peter Brown Nov 29 '11 at 00:56

2 Answers2

3

I'm not sure if there's something built into Rails for this, but there is a library that can do it. It may even come with Rails since I have it but did not install it separately:

require 'htmlentities'

HTMLEntities.new.decode("&gt;") # =>  ">"
HTMLEntities.new.encode(">") # => "&gt;"
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • Hey Beerlington. Thanks for the answer. I think I will check if the other one works first, just to avoid installing a gem over this. BUT his isn't working yet, so yours may be the answer! Thanks – jay Nov 29 '11 at 02:08
2

You could use CGI::unescapeHTML

> CGI::unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
=> "Usage: foo \"bar\" <baz>"

*Edit: +1 @Beerlington for htmlentities. I always forget it exists.

numbers1311407
  • 33,686
  • 9
  • 90
  • 92