如何在 PHP 中正确对字符串进行 URL 编码?

2023-12-26

我正在创建一个搜索页面,您可以在其中输入搜索查询并将表单提交到search.php?query=your query。哪个 PHP 函数是最好的,我应该使用它来编码/解码搜索查询?


对于 URI 查询值使用urlencode http://php.net/urlencode/urldecode http://php.net/urldecode;用于其他用途rawurlencode http://php.net/rawurlencode/rawurldecode http://php.net/rawurldecode.

要创建整个查询字符串,请使用http_build_query() https://php.net/http_build_query

和...之间的不同urlencode and rawurlencode is that

  • urlencode编码根据应用程序/x-www-form-urlencoded http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1(空间编码为+) while
  • rawurlencode根据明文编码百分比编码 https://www.rfc-editor.org/rfc/rfc3986#section-2.1(空间编码为%20).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 PHP 中正确对字符串进行 URL 编码? 的相关文章

随机推荐