php encode full url with spaces in arguments

一世执手 提交于 2019-12-08 02:28:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!