为什么 PHP 中的 rawurlencode() 会向 & 符号添加额外的转义字符?
我想我在这里遗漏了一些明显的东西,但这让我发疯,我无法弄清楚。我正在开发一个 WordPress 插件,其中一部分需要获取 WordPress 帖子标题并将其发送到 RESTful Web 服务以执行其他操作。所以我当然想对帖子标题进行 rawurlencode() ,因为谁知道里面可能有什么文本。然而,由于某种原因,我得到的输出有额外的转义字符,我不知道它们来自哪里(这显然会导致我调用的 Web 服务出现问题)。
我的代码相当简单:
$topic = get_the_title($post_id);
$curl_post_fields = 'name=' . rawurlencode( $topic );
然而,当我打印这两个字符串的输出时,我得到:
topic=a & b
name=a%20%26%23038%3B%20b
虽然我期望 URL 编码的字符串是,但
name=a%20%26%20b
我不知道额外的 %23038%3B 可能来自哪里。如果我正确读取编码,它会转换为#038;但我还是不知道它从哪里来。
I think I'm missing something obvious here but it is driving me crazy and I can't figure it out. I'm developing a WordPress plugin and part of it needs to take the WordPress post title and send that to a RESTful web service to do something else. So of course I want to rawurlencode() the post title since who knows what text might be in there. However, for some reason the output I'm getting has extra escape characters and I have no idea where they are coming from (and it's causing problems with the web service I'm calling obviously).
My code is fairly straight forward:
$topic = get_the_title($post_id);
$curl_post_fields = 'name=' . rawurlencode( $topic );
Yet when I print the output of those two strings I get:
topic=a & b
name=a%20%26%23038%3B%20b
Whereas I would expect the URL encoded string to be
name=a%20%26%20b
I have no idea where that extra %23038%3B could be coming from. If I'm reading the encoding on that correctly it translates to #038; but I still don't know where it's coming from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之间似乎也有一个 html 编码,而不是
&
,&
位于编码字符串中。可能是因为 &必须在 html 中转义,并且 get_title 函数使用html_special_chars
或类似的东西转义它。There seems to be a html encoding in between as well, instead of
&
,&
is in the encoded string. Probably because & has to be escaped in html, and the get_title function escapes this usinghtml_special_chars
or something like that.当我使用较旧的 php 版本时,我遇到了一些问题
I had some problems with that when i used an older php version