php用参数中的空格编码完整的url

问题描述 投票:2回答:3

我有一个看起来像这样的网址

http://example.com/index.php?id=111&title=word1 word2 word3

在使用curl请求url之前,我需要对空格进行编码。

但是如何对网址的正确部分进行urlencode。

[urlencoderawurlencode编码https问号等的斜杠。

php urlencode
3个回答
5
投票

您可以使用parse_url()将URL分成各个部分。使用urlencode()返回的数组中query元素上的parse_url,然后使用http_build_url将其放回原处。

注意:http_build_url需要PECL pecl_http> = 0.21.0


0
投票

用“ +”代替空白。


0
投票

尝试一下此功能,我做了一点测试,效果很好https://gist.github.com/volandku/8000c7deb654df93ebd181e40b52be97

© www.soinside.com 2019 - 2024. All rights reserved.