首页 > ruby > ruby的url解码函数和url编码函数

ruby的url解码函数和url编码函数

2010年1月5日 leleba 发表评论 阅读评论

含中文字符的url是一些乱码,如中文”个”,在url中表示为”%b8%f6″。有时需要进行url解码和编码,两个函数如下:

def URLDecode(str)
str.gsub!(/%[a-fA-F0-9]{2}/) { |x| x = x[1..2].hex.chr }
end

def URLEncode(str)
str.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format(%%%x, x[0]) }
end



分类: ruby 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
      
                  
      

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word