PHP 中的 URL 解码

发布于 2024-08-12 09:12:24 字数 380 浏览 8 评论 0原文

我正在尝试使用 PHP 的 urldecode 函数解码此 URL 字符串:

urldecode("Ant%C3%B4nio+Carlos+Jobim");

这应该输出...

'Antônio Carlos Jobim'

...但相反,

'Antônio Carlos Jobim'

我已经在 基于JS的在线解码器取得了巨大成功,但似乎无法在服务器端执行此操作。有什么想法吗?

I am trying to decode this URL string using PHP's urldecode function:

urldecode("Ant%C3%B4nio+Carlos+Jobim");

This is supposed to output...

'Antônio Carlos Jobim'

...but instead is ouptutting this

'Antônio Carlos Jobim'

I've tested the string in a JS-based online decoder with great success, but can't seem to do this operation server side. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我只土不豪 2024-08-19 09:12:24

您的字符串也是 UTF-8 编码的。这将起作用:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

输出:“Antônio Carlos Jobim”。

Your string is also UTF-8 encoded. This will work:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

Output: "Antônio Carlos Jobim".

爱殇璃 2024-08-19 09:12:24

实际上,您得到了所需的输出,但它没有被解释为 UTF-8。如果这是在 HTTP 应用程序上,您应该发送标头或元标记(或两者),告诉客户端使用 UTF-8。

编辑:例如:

// replace text/html with the content type you're using
header('Content-Type: text/html; charset=UTF-8');

Actually, you get the desired output, but it is not interpreted as UTF-8. If this is on an HTTP application, you should send a header or a meta tag (or both) which would tell the client to use UTF-8.

Edit: for example:

// replace text/html with the content type you're using
header('Content-Type: text/html; charset=UTF-8');
如若梦似彩虹 2024-08-19 09:12:24

当我

<?php
echo urldecode("Ant%C3%B4nio+Carlos+Jobim");
?>

在浏览器中正确显示时,就像

安东尼奥·卡洛斯·若比姆

我已经使用 XAMPP 进行了测试

when I do

<?php
echo urldecode("Ant%C3%B4nio+Carlos+Jobim");
?>

Its display correctly in my browser like

Antônio Carlos Jobim

I have tested with XAMPP

姐不稀罕 2024-08-19 09:12:24

另一种选择是:

<?php
$smthing = 'http%3A%2F%2Fmysite.com';
$smthing = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($smthing)); 
$smthing = html_entity_decode($smthing,null,'UTF-8');
echo $smthing;
?>

输出变为:http://mysite.com

another option is:

<?php
$smthing = 'http%3A%2F%2Fmysite.com';
$smthing = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($smthing)); 
$smthing = html_entity_decode($smthing,null,'UTF-8');
echo $smthing;
?>

output becomes : http://mysite.com

筱果果 2024-08-19 09:12:24

在将其回显到页面之前,您是否也在使用 htmlenteties ?当我刚刚测试您的代码时,仅使用 urldecode("Ant%C3%B4nio+Carlos+Jobim"); 部分就可以正常工作,但是当我通过 htmlentities 运行它时我得到了和你一样的输出。

这似乎是 UTF-8 字符以及 PHP 如何处理 htmlentities 函数的问题。

Are you also using htmlenteties before echoing it to the page? When I just tested your code it worked fine with just the urldecode("Ant%C3%B4nio+Carlos+Jobim"); part, but when I ran it through htmlentities I got the same output as you did.

It seems to be a problem with the UTF-8 characters and how PHP handles the htmlentities function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文