Skip to content

Instantly share code, notes, and snippets.

@ocean
Created June 10, 2009 09:07
Show Gist options
  • Save ocean/127107 to your computer and use it in GitHub Desktop.
Save ocean/127107 to your computer and use it in GitHub Desktop.
Explanation
I'm trying to grab a string from a database, which may any type of characters in it, and display it as HTML.
Therefore need to replace characters like 1/2 signs and curly quotes with their respective HTML character entities.
EDIT: Unfortunately, I've just found that the code snippet below works fine, it must be the way I'm calling the
method in the full script, which is much larger, sigh.
def html_entity_escape(string)
fixed = string.to_s.gsub(/&/, "&").gsub(/½/, "½").gsub(/¼/, "¼").gsub(/¾/, "¾").gsub(/“/, "“").gsub(/”/, "”").gsub(/’/, "’").gsub(/‘/, "‘")
#fixed = string.to_s.gsub(/38/, "&").gsub(/189/, "½").gsub(/188/, "¼").gsub(/190/, "¾").gsub(/8220/, "“").gsub(/8221/, "”").gsub(/8217/, "’").gsub(/8216/, "‘")
return fixed
end
blahblah = "only 3 breaks of ½ hour, worked for 16 ¼ hours, had a break of 8 ¼ hours"
puts html_entity_escape(blahblah)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment