开发者

$_GET encoding problem with cyrillic text

开发者 https://www.devze.com 2022-12-22 00:27 出处:网络
I\'m trying this code (on my local web server)开发者_如何学Python <?php echo \'the word is / думата е \'.$_GET[\'word\'];

I'm trying this code (on my local web server)开发者_如何学Python

<?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...


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.


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).


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

Cheers!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号