Search code examples
phpurlencode

php encode full url with spaces in arguments


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.


Solution

  • 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