when to encode space to plus (+) and when to %20 ?   Leave a comment

+ means a space only in application/x-www-form-encoded content, such as the query part of a URL:

http://www.example.com/path/foo+bar/path?query+name=query+value

In this URL, the parameter name is query name with a space and the value is query value with a space, but the folder name in the path is literally foo+barnot foo bar.

%20 is a valid way to encode a space in either of these contexts. So if you need to URL-encode a string for inclusion in part of a URL, it is always safe to replace spaces with %20 and pluses with %2B. This is what eg. encodeURIComponent() does in JavaScript. Unfortunately it’s not what urlencode does in PHP (rawurlencode is safer).

 

The browser will create a query+name=query+value parameter from a form with <input name="query name" value="query value">. It will not create query%20name from a form, but it’s totally safe to use that instead, eg. if you’re putting a form submission together youself for an XMLHttpRequest. If you have a URL with a space in it, like <a href="http://www.example.com/foo bar/">, then the browser will encode that to %20 for you to fix your mistake, but that’s probably best not relied on. – bobince Apr 20 ’10 at 21:22

 

 

Posted 2011年01月25日 by gw8310 in 未分类

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: