本文介绍了urlencode与rawurlencode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用变量创建URL,我有两种选择来编码字符串. urlencode()rawurlencode().

到底有什么区别,哪个是首选?

解决方案

这取决于您的目的.如果与其他系统的互操作性很重要,那么rawurlencode似乎是必经之路.一个例外是传统系统,该系统希望查询字符串遵循以+而不是%20(在这种情况下需要urlencode)编码的空格的表单编码样式.

rawurlencode 遵循PHP 5.3.0之前的RFC 1738,之后遵循RFC 3986(请参见 http://us2.php.net/manual/en/function.rawurlencode.php )

关于RFC 3986与1738的说明.php5.3之前的rawurlencode根据RFC 1738编码了波浪号字符(~).但是,从PHP 5.3开始,rawurlencode遵循RFC 3986,它不需要对波浪号字符进行编码. >

urlencode 将空格编码为加号(而不是像rawurlencode中那样作为%20编码)(请参见 http://us2.php.net/manual/zh/function.urlencode.php )

这与 RFC 1866 中的application/x-www-form-urlencode的定义相对应. a>.

其他阅读内容:

您可能还希望在 http://bytes.com上查看讨论. /groups/php/5624-urlencode-vs-rawurlencode .

此外, RFC 2396 也值得一看. RFC 2396定义了有效的URI语法.我们感兴趣的主要部分来自3.4查询组件:

如您所见,+是查询字符串中的保留字符,因此需要根据RFC 3986(如rawurlencode)进行编码.

If I want to create a URL using a variable I have two choices to encode the string. urlencode() and rawurlencode().

What exactly are the differences and which is preferred?

解决方案

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 follows RFC 1738 prior to PHP 5.3.0 and RFC 3986 afterwards (see http://us2.php.net/manual/en/function.rawurlencode.php)

Note on RFC 3986 vs 1738. rawurlencode prior to php 5.3 encoded the tilde character (~) according to RFC 1738. As of PHP 5.3, however, rawurlencode follows RFC 3986 which does not require encoding tilde characters.

urlencode encodes spaces as plus signs (not as %20 as done in rawurlencode)(see http://us2.php.net/manual/en/function.urlencode.php)

This corresponds to the definition for application/x-www-form-urlencoded in RFC 1866.

Additional Reading:

You may also want to see the discussion at http://bytes.com/groups/php/5624-urlencode-vs-rawurlencode.

Also, RFC 2396 is worth a look. RFC 2396 defines valid URI syntax. The main part we're interested in is from 3.4 Query Component:

As you can see, the + is a reserved character in the query string and thus would need to be encoded as per RFC 3986 (as in rawurlencode).

这篇关于urlencode与rawurlencode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:18