Return to Snippet

Revision: 9457
at November 7, 2008 19:03 by chrisaiv


Initial Code
require 'cgi'


##################################
#Method 1: Easy Breezy
##################################
url = "http%3A%2F%2Fcdn.buzznet.com%2Fassets%2Fvideox%2F3%2F6%2F9%2F6%2F6%2F3%2F1%2Fvid-3696631.flv"

url = CGI.unescape( url )
puts url

##################################
#Method 2: Complicated but with more control
##################################

def url_escape(string)
  string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.tr(' ', '+')
end

def url_unescape(string)
  string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
    [$1.delete('%')].pack('H*')
  end
end

puts url_unescape( url )

Initial URL


Initial Description
I needed a quick way to unescape a URL Encoded string.  I found 2 chill ways

Initial Title
Ruby: Unescape a URLEncoded String

Initial Tags
ruby

Initial Language
Ruby