DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Unicode Characters To HTML Entities In Ruby

01.16.2006
| 12967 views |
  • submit to reddit
        
def entities( str )
  converted = []
  str.split(//).collect { |c| converted << ( c[0] > 127 ? "&##{c[0]};" : c ) }
  converted.join('')
end
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 1:18pm

Your code doesn't work for UTF8 strings, with multibytes per char, e.g. check for: str = "παÏ?άνοια" str.unpack("U*").collect {|s| (s > 127 ? "&##{s};" : s.chr) }.join("") :)