/ Published in: Ruby
I needed a quick way to unescape a URL Encoded string. I found 2 chill ways
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 )