西里尔文文本的 $_GET 编码问题

发布于 2024-08-24 18:18:36 字数 350 浏览 4 评论 0原文

我正在尝试这段代码(在我的本地网络服务器上),

<?php
echo 'the word is / думата е '.$_GET['word'];
?>

但是当输入 ?word=проба 时,我得到了损坏的结果

the word is / думата е ����

该文档保存为“UTF-8 without BOM”,标题也是 UTF-8 。 我尝试过 urlencode() 和 urldecode() 但效果是一样的。 将其上传到网络服务器时,工作正常...

I'm trying this code (on my local web server)

<?php
echo 'the word is / думата е '.$_GET['word'];
?>

but I get corrupted result when enter ?word=проба

the word is / думата е ����

The document is saved as 'UTF-8 without BOM' and headers are also UTF-8.
I have tried urlencode() and urldecode() but the effect was same.
When upload it on web server, works fine...

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

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

发布评论

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

评论(5

落叶缤纷 2024-08-31 18:18:36

如果您尝试发送 HTTP Content-type 标头来指示浏览器您的页面正在生成哪种编码/字符集,该怎么办?

例如,类似这样的东西可能会有所帮助:

header('Content-type: text/html; charset=UTF-8');
echo 'the word is / думата е '.$_GET['word'];

当然,这是如果您正在生成 HTML - 您可能是这样。

考虑到服务器级别有一个配置设置定义了默认发送哪种编码,也许您服务器上的默认编码是可以的,而本地服务器上的默认编码则不行。

自己发送这样的标头可以解决问题:它将确保编码始终设置正确。

What if you try sending a HTTP Content-type header, to indicate the browser which encoding / charset your page is generating ?

For instance, something like this might help :

header('Content-type: text/html; charset=UTF-8');
echo 'the word is / думата е '.$_GET['word'];

Of course, this is if you are generating HTML -- you probably are.

Considering there is a configuration setting at the server's level that defines which encoding is sent by default, maybe the default encoding on your server is OK -- while the one on your local server is not.

Sending such a header by yourself would solve the problem : it would make sure the encoding is always set properly.

硬不硬你别怂 2024-08-31 18:18:36

我想您正在使用 Apache Web 服务器。

Apache 配置有一个常见问题 - 配置中带有“AddDefaultCharset”的行应该被注释掉(在行的开头添加 #,或用“AddDefaultCharset off”替换该行),因为它“覆盖了中给出的任何编码”元 http-equiv 或 xml 编码标记中的文件”。

在我当前的安装(Apache2 @ Ubuntu Linux)中,该行位于“/etc/apache2/conf.d/charset”中,但在其他(Linux/Unix)设置中可以位于“/etc/apache2/httpd.conf”中,或“/etc/apache/httpd.conf”(如果您使用的是 Apache 1)。如果在这些文件中找不到它,可以使用“cd /etc/apache2 ; grep -r AddDefaultCharset *”(适用于 Apache 2 @ Unix/Linux)进行搜索。

I suppose you are using the Apache web server.

There is a common problem with Apache configuration - a line with "AddDefaultCharset" in the config should be commented out (add # in the begining of the line, or replace the line with "AddDefaultCharset off") because it "overrides any encoding given in the files in meta http-equiv or xml encoding tags".

In my current installation (Apache2 @ Ubuntu Linux) the line is found in "/etc/apache2/conf.d/charset" but in other (Linux/Unix) setups can be in "/etc/apache2/httpd.conf", or "/etc/apache/httpd.conf" (if you are using Apache 1). If you don't find it in these files you can search for it with "cd /etc/apache2 ; grep -r AddDefaultCharset *" (for Apache 2 @ Unix/Linux).

一口甜 2024-08-31 18:18:36

查看更改服务器编码。非常好的读物!

干杯!

Take a look at Changing the server encoding. An excellent read!

Cheers!

归途 2024-08-31 18:18:36

如果您从 AJAX 收到 $_GET,请确保您的 blablabla.js 文件采用 UTF-8 编码。您还可以使用 iconv("cp1251","utf8",$_GET['word']); 以 UTF-8 显示 $_GET['word']

If You recieve $_GET from AJAX make sure that Your blablabla.js file in UTF-8 encode. Also You can use iconv("cp1251","utf8",$_GET['word']); to display your $_GET['word'] in UTF-8

我为君王 2024-08-31 18:18:36

我刚刚遇到了这个问题,如果您使用 htmlentities() 过滤 GET 变量,有时会发生这种情况。看起来这个函数将西里尔字符转换成奇怪的东西。

I just had the issue and it sometimes happens if you filter the GET variable with htmlentities(). It seems like this function converts cyrillic characters into weird stuff.

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