PHP URL encoding retaining invalid url characters

2019-02-27 13:18发布

I need to replace url invalid characters with something url valid then convert it back again.

This is for a search page with a url like http://my.site/search/this-is-a-search, the search form POSTS then the user redirected to the new url.

Php has functions urlencode and urldecode however these do not work at all, and leave invalid characters in my url.

Surely I don't need to reinvent the wheel here.

标签: php http
2条回答
一纸荒年 Trace。
2楼-- · 2019-02-27 13:53

For stuff like http://my.site/search/this-is-a-search (i.e., outside the query string), you should use rawurlencode and rawurldecode. These are guaranteed to be URL-safe.

However, urlencode will never generate an unsafe URL path either, since the only difference is how spaces are encoded and urlencode encodes spaces to +, which is permitted in the URL path.

From RFC 1738:

httpurl        = "http://" hostport [ "/" hpath [ "?" search ]]
hpath          = hsegment *[ "/" hsegment ]
hsegment       = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
uchar          = unreserved | escape
unreserved     = alpha | digit | safe | extra
safe           = "$" | "-" | "_" | "." | "+"
查看更多
可以哭但决不认输i
3楼-- · 2019-02-27 14:02

What about rawurlencode().

http://www.php.net/manual/en/function.rawurlencode.php

echo rawurlencode('http://my.site/search/this-is-a-search');
// http%3A%2F%2Fmy.site%2Fsearch%2Fthis-is-a-search
查看更多
登录 后发表回答