2015年11月5日 星期四

PHP urlencode() 和 rawurlencode()的差異


It will depend on your purpose. If interoperability with other systems is important then it seems rawurlencode is the way to go. The one exception is legacy systems which expect the query string to follow form-encoding style of spaces encoded as + instead of %20 (in which case you need urlencode).
這將取決於你的目的。如果與其他系統的互操作性是很重要的話,使用rawurlencode。
除非是舊系統的查詢字符串(query string)把空白視為+而不是%20,在這種情況下,才需要使用urlencode()

rawurlencode follows RFC 1738 prior to PHP 5.3.0 and RFC 3986 afterwards
rawurlencode 在PHP5.3.0之前遵循RFC 1738、之後遵循RFC3986
$ php -r 'echo urlencode(" ");'
+

urlencode encodes spaces as plus signs (not as %20 as done in rawurlencode)
urlencode 把空白編碼成加號,而不是rawurlencode 的%20
$ php -r 'echo rawurlencode(" ");'
%20

結論:
So which is preffered?
哪一種是首選
rawurlencode. go with the standard in this case. urlencode is only kept for legacy use
rawurlencode. 是標準,urlencode只是給舊版用的

參考資料:
http://stackoverflow.com/questions/996139/urlencode-vs-rawurlencode  urlencode vs rawurlencode?



沒有留言:

張貼留言