Tuesday, June 26, 2007

url_encode and url_decode of Ruby


require "ERB"
require "uri"

old = "http://www.google.com/search?q=搜索&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"
# URI.escape(old):
new = " http://www.google.com/search?q=%CB%D1%CB%F7&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"
# ERB::Util.url_encode(old)与php中url_encode()相同:
url_encoded = "http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3D%CB%D1%CB%F7%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dorg.mozilla%3Aen-US%3Aofficial%26client%3Dfirefox-a"
# javascript encodeURIComponent: http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3D%E6%90%9C%E7%B4%A2%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dorg.mozilla%3Aen-US%3Aofficial%26client%3Dfirefox-a
# 跟url_encode中文字符的结果不同


puts URI.escape(old)
puts ERB::Util.url_encode(old)


puts URI.unescape(url_encoded)
puts URI.unescape(new)




No comments :