UTF-8 not working outside the header - php

Utf-8 works just for text generated in header, but not for text in (for example) index. Does anyone have an idea how to fix that?
header.php:
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
....
for example: index.php
<?php
include "header.php";
?>
...body stuff
Thanks :)

Check your file encoding. I don't know what editor you usually use, but in Eclipse you can have a look to in via right click on a file -> properties
For database, check the encoding of each fields & be sure to call "set names utf8" query each time you open a mysql connexion

Related

Dropdown is not listing French characters

I have a dropdown list, which lists things that has french characters in it. Although I see characters like this:
MySQL database is set to UTF-8. So, there is no problem in database. I've checked the database and characters are okay.
I also added these lines to my header.php but still doesn't work.
header('Content-Type: text/html; charset=utf-8');
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
What else should I do?
EDIT
I can print french letters everywhere in the site but still dropdown doesn't work.
SOLUTION
First of all, if you would like to use utf-8 in your site you should add this right after you connect to database:
mysqli_set_charset($connectionName, 'utf8mb4');
Second, you should also add this in your header tags:
<meta charset="utf-8">
That's all. After all these, my problem still wasn't fixed until I figure out my stupidity. I have header.php file, which is included on top of every page but there is another php file, which outputs dropbox options from database. So, this file don't need header.php at top. So, I had to add mysqli_set_charset($connectionName, 'utf8mb4'); specially for that page.

UTF-8 html5 webpage encoding

I'm trying to grasp UTF-8 and charset encoding in general, but it's tricky
This code:
<?php
header('Content-type: text/html; charset=utf-8');
?>
<!doctype html>
<html lang="da">
<head>
<title>UTF-8 test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<p>UTF-8 æøå tést Señor!</p>
</div>
</body>
</html>
Outputs:
UTF-8 ��� t�st Se�or!
Same result on both local server and on multiple public sites (all Apache)
Do I need to change something in php? Or Apache? Or my text editor? (notepad++)
Your text editor isn't saving as UTF-8.
I think you should change the character encoding of your notepad++, you can change this under "Encoding" menu.
you can check your right corner of status bar for the encoding of your file.

Charset=utf8 not working in my PHP page

I got a problem with my character encoding. Whenever I load a page in the browser, It shows like this:
So I need to manually configure it by the browser. Thanks for your help.
sounds like you don't serve your content as utf-8. do this by setting the correct header:
header('Content-type: text/html; charset=utf-8');
in addition to be really sure the browser understands, add a meta-tag:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
note that, depending on where the text comes from, you might have to check some other things too (database-connection, source-file-encoding, ...) - i've listed a lot of them in one of my answers to a similar question.
As stated by kraikkonen85 in this comment:
Besides setting mysql_set_charset and adding utf8 setting metadata
to html, another important but "unpredictable" reason is the encoding
type of your PHP editor when saving. For example, if you save the php
file other than encodings such as "unicode" or "ANSI", as i
experienced, you may notice weird chars like squares, question marks
etc.
To make sure, try "save as" method to see explictly that your php file
is being saved as utf8.
it may be because your content might not utf-8 you can set utf-s by setting the header and meta
header('Content-type: text/html; charset=utf-8');
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

Require UTF-8 php file

I'm experiencing very strange problem regarding encoding PHP files in UTF-8. For example, I have two files: index.php and require.php. In index.php files I have this code
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<?php
require 'require.php';
?>
<body>
<a></a>
</body>
</html>
In require.php I only have empty PHP tags
<?php
?>
When I open index.php and use Chrome's Element Inspector to see the output HTML I get this:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
"
"
<a></a>
</body>
</html>
Note the two strange quote marks at the begining of body. If I remove the require statement from PHP code or simply remove a tags they dissapear. What's even stranger is that if I echo something out in require.php file it gets outputed between those two quotes.
The problem goes away if I change encoding from UTF-8 to ANSI in require.php
I've been searching for answer for hours and did not found single person who has same problem.
Try add header('Content-type: text/html; charset=utf-8'); in index.php, and remove the ending '?>' from require.php.
Made worse after adding the header. Although, converting the file to UFT-8 Without BOM is worked for me! Thank you.
If you are sending a plaintext file, you will need this...
<?php
header('Content-type: text/plain; charset=utf-8');
print('中文。');
?>
For sending HTML, you'll need to change the header's "text/plain" to "text/html".

PHP Japanese echo string becomes question marks

I'm working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can't get the characters to display if I use a function like this:
echo.php:
<?php
function draw(){
echo "日本語";
}
draw();
?>
I get "日本語"
but if I try this :
index.php:
<?php
some stuff
include "echo.php";
draw();
?>
I get "???".
Is there any way for me to get my echo file to send the data to the first file in a way it can read it?
EDIT:
The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file
Here's the code:
<html lang="ja">
<head>
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>
Where layout.php is just a file with a list of links, I have this on each page so the links are on every page.
For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.
You should change your file encoding to UTF-8 and set the header on the website to UTF-8.
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
in HTML5 you should use:
<meta charset="utf-8" />
or in php
header('content-type: text/html; charset=utf-8');
You problem definitely has something to do with the character encoding. Try to check the following:
All your strings in all PHP scripts are Unicode (UTF-8 is a very common choice). Use utf8_encode() and/or utf8_decode() to force UTF-8 on your strings where necessary.
Your server sends PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped data)
Your browser understands and accepts Unicode (UTF-8). Typically browser would send Accept-Charset: UTF-8,*;q=0.5 in the GET request to hint it's Unicode capability.
Finally, I have checked your code with PHP version 5.3.6 and Firefox, Chrome and IE 9. Firefox and Chrome prints the Japanese characters as expected. It's only IE 9 which doesn't print it correctly. Snooping on the GET request from IE reveals, it is indeed not sending any Accept-Charset: UTF-8,*;q=0.5. I am not entirely sure how to force IE to send that because in my case, clearly my server (Apache) together with PHP was definitely sending UTF-8 string back.
One more hint - you may try converting your strings into HTML entities. For example - echo "©"; would print ©. But, I am not 100% sure how to convert all strings into HTML entities using PHP. I have unsuccessfully attempted with htmlentities() and htmlspecialchars(). But, it didn't change anything for IE.
i have same problem. But i handle it.
convert "echo.php"'s encoding to EUC-JP or SHIFT-JIS.
<?php
function draw(){
echo mb_convert_encoding("日本語", 'UTF-8', array('EUC-JP', 'SHIFT-JIS', 'AUTO'));
}
draw();
?>
This works for me :)
That happens when charset is not defined or is incorrect.
You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.
HTML 4
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
HTML 5
<meta charset="utf-8" />
add below meta tags to header
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
MySQL and PHP need to be handling the same character set.
Try adding ..
mysqli_set_charset("utf8");
.. after connecting to your database. Always works for me!
If you are using php and mssql you will also need to specify characterset in the mssql connection string.
Like this:
$connectionInfo = array("Database"=>"db name", "UID"=>"username", "PWD"=>"pw","CharacterSet" => "UTF-8");
$serverName = "server";
$conn = sqlsrv_connect($serverName, $connectionInfo);
Please note you still need to do the things suggested above
declare your html charset like this HTML 5:
<meta charset="utf-8" />
php charset like this:
header('content-type: text/html; charset=utf-8');

Categories