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

标签 php urlencode

我有一个看起来像那样的 url

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

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

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

urlencoderawurlencodehttps 的斜线、问号等进行编码

最佳答案

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

注意:http_build_url 需要 PECL pecl_http >= 0.21.0

关于php用参数中的空格编码完整的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9922407/

相关文章:

javascript - Ob_end_flush 不断产生错误

php - 来自 4 个表的复杂 mysql 查询

php - GuzzleHttp 禁用 UrlEncoding

ASP.NET WebAPI 将 urlencoded 正文中的空字符串作为 null 传递

javascript - 为什么 javascript 协议(protocol)会自动解码 URL?

javascript - URLEncode - 让它按照我想要的方式工作

python-3.x - python3请求使用quote而不是quote_plus

php - 选择特定行的 Mysql 数据

PHP 从 MySQL 数据库中搜索 FUL​​LTEXT 转义字符串

php - 添加 .htaccess 尾部斜杠和 css 变得糟糕