local server ruins character encoding - php

I need to learn all these encoding stuffs well. Because this is the third time I'm wasting my time with silly wrong encoding problems. Here is the problem:
I have a simple php file.
File is in the format of UTF-8
If I run my local server, it makes ı => ı and ö => Ãœ
If I rename extension as HTML it works perfectly, so the problem is local server, definetely.
To correct this issue, I have done the following
I've read this, this and this
Double checked the file encoding, it's UTF-8
Added the meta tag <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Added the header inside php tag: header("Content-type:text/html; charset: UTF-8");
Added the internal encoding inside php tag mb_internal_encoding('UTF-8');
Corrected the line in php.ini file default_charset = UTF-8
Added the following in httpd.conf file: AddDefaultCharset utf-8
Everything is hard-coded, so I don't use database, it's not related to mysql encoding
I'm using WAMP, and machine is Windows 7 English. I'm completely exhausted, therefore, I really need help.
Thanks.

Checks whether the document encoding is UTF-8 without BOM
and if does not work try utf8_encode() and utf8_decode()
EDIT
$text = "A strange string to pass, maybe with some ø, æ, å characters.";
foreach(mb_list_encodings() as $chr){
echo mb_convert_encoding($text, 'UTF-8', $chr)." : ".$chr."<br>";
}

In the end I found problem. I wrote
header("Content-type:text/html; charset: UTF-8");
After Content-Type, we need to have put : but after Charset we need to put equal sign = which was very frustrating for me. So,
header('Content-Type: text/html; charset=utf-8');
solves the problem.

Related

German special character displayed wrong on Firefox [duplicate]

I have these Chinese characters:
汉字/漢字''test
If I do
echo utf8_encode($chinesevar);
it displays
??/??''test
Or even if I just do a simple
echo $chinesevar
it still displays some weird characters...
So how am I going to display these Chinese characters without using the <meta> tag with the UTF-8 thingy .. or the ini_set UTF-8 thing or even the header() thing with UTF-8?
Simple:
save your source code in UTF-8
output an HTTP header to specify to your browser that it should interpret the page using UTF-8:
header('Content-Type: text/html; charset=utf-8');
Done.
utf8_encode is for converting Latin-1 encoded strings to UTF-8. You don't need it.
For more details, see Handling Unicode Front To Back In A Web App.
Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8
HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
in PHP:
header('Content-Type: text/html; charset=utf-8');
And if you work with a database look that your database is in UTF-8 if you read the text from your database.
$chinesevarOK = mb_convert_encoding($chinesevar, 'HTML-ENTITIES', 'UTF-8');
Perhaps take a look at the following solutions:
Your database, table and field COLLATE should be utf8_unicode_ci
Check if your records are showing the correct characters within the database...
Set your html to utf8
Add the following line to your php after connecting to the database
mysqli_set_charset($con,"utf8");
http://www.w3schools.com/php/func_mysqli_set_charset.asp
save your source code in UTF-8 No BOM

Yet another character encoding issue

The language is Php. The editor is Php Storm. The editor encoding is utf-8. The file encoding also. mb_detect_encoding() also returns that the encoding is utf-8 but php does not recognizes č, ć, ž, đ and others. Does anyone know what the problem is?
I know that this is yet another character encoding question and that the solution is never clear in this case, but thank you for any answer.
EDIT
I use my own php framework and the index.php file is encoded to ANSI, not utf-8, but the rest of the files are utf-8. If I try to change from ANSI to utf-8, I get a content encoding
error.
I solved the problem with these lines of code:
ini_set('mbstring.internal_encoding','UTF-8');
ini_set( 'default_charset', 'UTF-8' );
ini_set('mbstring.func_overload',7);
header('Content-Type: text/html; charset=UTF-8');
Actually, setting the 'default_charset' is enough to work but since I spent the last 4 hours trying to solve this, the hell with it. Let it all fire.

Weird characters though using the right encoding

I work on a website that has different language interfaces, so far I use english and german.
when the german text is loaded, it shows weird characters like the following screenshot
though I use
header('Content-type: text/html; charset=utf-8');
and also in the html header
<META http-equiv="content-type" content="text/html; charset=utf-8">
what else can I do to solve it ?
Thanks
The content of the page needs to also be in UTF-8. Your content was probably made using MS Word, which uses Windows 1251 encoding. You need to re-save your document as UTF-8.
UTF-8 does not convert formats for you.
If those strings are saved in a file, the file has to be encoded in UTF-8 too.
If you're getting them from a database, they'll have to be stored as UTF-8 and you'll have to set the connection charset to utf-8.
You could also check whether your text is UTF-8 and if not, convert it with utf8_encode.

How to properly display Chinese characters in PHP?

I have these Chinese characters:
汉字/漢字''test
If I do
echo utf8_encode($chinesevar);
it displays
??/??''test
Or even if I just do a simple
echo $chinesevar
it still displays some weird characters...
So how am I going to display these Chinese characters without using the <meta> tag with the UTF-8 thingy .. or the ini_set UTF-8 thing or even the header() thing with UTF-8?
Simple:
save your source code in UTF-8
output an HTTP header to specify to your browser that it should interpret the page using UTF-8:
header('Content-Type: text/html; charset=utf-8');
Done.
utf8_encode is for converting Latin-1 encoded strings to UTF-8. You don't need it.
For more details, see Handling Unicode Front To Back In A Web App.
Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8
HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
in PHP:
header('Content-Type: text/html; charset=utf-8');
And if you work with a database look that your database is in UTF-8 if you read the text from your database.
$chinesevarOK = mb_convert_encoding($chinesevar, 'HTML-ENTITIES', 'UTF-8');
Perhaps take a look at the following solutions:
Your database, table and field COLLATE should be utf8_unicode_ci
Check if your records are showing the correct characters within the database...
Set your html to utf8
Add the following line to your php after connecting to the database
mysqli_set_charset($con,"utf8");
http://www.w3schools.com/php/func_mysqli_set_charset.asp
save your source code in UTF-8 No BOM

PHP Japanese Strings getting set to?

I have a PHP file with one simple echo function:
echo 'アクセスは撥ねりません。';
but when I access that page i get this:
????????????
Can someone help me?
I also have my page encoding set to UTF-8, and I know it, because all of the browsers i used said so.
I also do this before the echo function:
mb_internal_encoding('UTF-8');
What does this do?
Does it help me?
All I need is to be able to echo a static Japanese string.
Thanks!
There are a few places where this could go wrong.
Firstly, if you aren't setting the output encoding in php with header()
header('Content-type: text/html; charset=utf-8');
or in your html with a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
you will need to check the php.ini setting default_charset. Chances are this is defaulted to iso-8859-1
Secondly, you may also need to check the content encoding you are saving the php script as. If you are saving it as ASCII or some other latin charset, it will munge the characters.
I got it.
I just had to set the mbstring extension settings to handle internal strings in UTF-8. Thas extension is standard with my build of PHP 5.3.0.
Maybe you are printing Japanese characters contained in UTF-16 (extended set of chars)?
I just did a quick test and your example works for me, so it's most likely one of these:
Your file is not saved in UTF-8, but some other encoding, such as Shift-JIS. A decent editor should be able to let you see what encoding it used
Your server is sending bad http headers. Can you use some tool to check the headers and paste the results? Or the results you got from the browser?
The browser is using an incompatible font
I saved a file in UTF-8, pasted your code into it, and my server is serving the file with Content-Type: text/html; charset=utf-8 and it shows up just fine. Did not need to use the mb_ function or anything else.

Categories