PHP URLDecode / UTF8_Encode 字符集特殊字符问题
我将英镑符号 £
传递到 PHP 页面,该页面已由 ASP URLEncode 为 %C2%A3
。
问题:
urldecode("%C2%A3") // £
ord(urldecode("%C2%A3")) // get the character number - 194
ord("£") // 163 - somethings gone wrong, they should match
这意味着当我执行 utf8_encode(urldecode("%C2%A3"))
时,我得到 £
但是执行 utf8_encode("£")< /code> 我按预期得到
£
我该如何解决这个问题?
I'm passing a pound symbol £
to a PHP page which has been URLEncoded by ASP as %C2%A3
.
The problem:
urldecode("%C2%A3") // £
ord(urldecode("%C2%A3")) // get the character number - 194
ord("£") // 163 - somethings gone wrong, they should match
This means when I do utf8_encode(urldecode("%C2%A3"))
I get £
However doing utf8_encode("£")
I get £
as expected
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你尝试一下,
你会发现,
因为这是 2 字节字符,而 ord() 返回第一个字符的值 (194 = Â)
if you try
you'll see
because this is 2-byte character and ord() returns value of first one (194 = Â)
我不认为
ord()
是多字节兼容的。它可能只返回字符串中第一个字符的代码,即 Â。在调用ord()
之前尝试对字符串进行utf8_decode()
处理,看看是否有帮助。I don't think
ord()
is multibyte compatible. It's probably returning only the code for the first character in the string, which is Â. Try toutf8_decode()
the string before callingord()
on it and see if that helps.关于 urldecode 和 UTF-8 的一些信息可以在 第一条评论中找到urldecode 文档。这似乎是一个已知问题。
Some infos about urldecode and UTF-8 can be found in the first comment of the urldecode documentation. It seems to be a known problem.
php.net 上针对 urlencode() 的第一条评论 < a href="http://www.w3.org/International/questions/qa-forms-utf-8.en.php" rel="nofollow noreferrer">解释了为什么会这样,并建议使用此代码更正它:
此外,您还应该决定是否希望发送到浏览器的最终 html 采用 utf-8 或其他编码,否则您的代码中将继续包含 £ 字符。
The first comment on php.net for urlencode() explains why this is and suggests this code for correcting it:
Also you should decide wether you want your final html you send to the browser to be in utf-8 or some other encoding, otherwise you will continue having £ characters in your code.