Never been to DZone Snippets before?

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

Convert unicode characters to HTML entities in Ruby (See related posts)

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

Comments on this post

dseverin posts on Jan 16, 2006 at 11:41
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("")

:)

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts