问题
I have a url that looks like that
http://example.com/index.php?id=111&title=word1 word2 word3
I need to encode the spaces before requesting the url with curl.
But how to urlencode the right parts of the url.
urlencode and rawurlencode encode the slashes of https the question mark etc.
回答1:
You can use parse_url() to split the URL into its respective pieces. Use urlencode() on the query element from the array that parse_url returns, then put it back together using http_build_url.
NOTE: http_build_url requires PECL pecl_http >= 0.21.0
回答2:
Replace white spaces with "+".
来源:https://stackoverflow.com/questions/9922407/php-encode-full-url-with-spaces-in-arguments