I need to convert a URL in Ruby which contains the character "ö".
In PHP, urlencode returns %F6 for ö which seems to be the Hex-value for "ö" in ISO 8859.
I tried several different methods but none of them returned the correct character:
What method should I use to get the desired output?
-e-
Additional requirement:
I only need to convert these characters in the path of the url. Colons, slashes etc. should remain the same:
http://example.com/this/is/an/ö
will be
Ruby uses UTF-8 strings by default:
str = 'ö'
str.encoding
#=> #<Encoding:UTF-8>
If you want an ISO 8859 encoded string in Ruby, you have to convert it:
str.encode('ISO-8859-1')
#=> "\xF6"
to URL-encode a string, there's CGI.escape
:
require 'cgi'
CGI.escape(str.encode('ISO-8859-1'))
#=> "%F6"
to encode an URL, use URI.escape
:
require 'uri'
url = 'http://example.com/this/is/an/ö'
URI.escape(url.encode('ISO-8859-1'))
#=> "http://example.com/this/is/an/%F6"