PHP Japanese echo line becomes question marks

I work on a php site that requires both Japanese and English. In most cases, I have no problem displaying the Japanese correctly, but I cannot get the characters to display if I use this function:

echo.php:

<?php function draw(){ echo "日本語"; } draw(); ?> 

I get "日本語"

but if I try: index.php:

 <?php some stuff include "echo.php"; draw(); ?> 

I get "???". Is there a way to get my echo file to send data to the first file the way it can read it?

EDIT: The website is running and showing Japanese characters correctly, unless it tries to pull a function from a php file. Here is 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 every page so that the links are on every page. For the other part, I have to use functions.php to get some data from the database and write it to the page, so I tried to put layout.php in function.php and call it: English links appeared, but Japanese links appeared as question marks .

+7
source share
7 answers

You must change the 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'); 
+7
source

Your problem is definitely related to character encoding . Try checking the following:

  • All of your lines in all PHP scripts are Unicode (UTF-8 is a very common option). Use utf8_encode() and / or utf8_decode() to force UTF-8 in your lines.
  • The server sends the PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped).
  • Your browser understands and accepts Unicode (UTF-8). Typically, the browser sends Accept-Charset: UTF-8,*;q=0.5 to the GET request to indicate its Unicode capabilities.

Finally, I checked your code with PHP version 5.3.6 and Firefox, Chrome, and IE 9. Firefox and Chrome print Japanese characters as expected. This is only IE 9 that does not print it correctly. Tracking the GET request from IE shows that it really does not send Accept-Charset: UTF-8,*;q=0.5 . I'm not quite sure how to get IE to send this, because in my case, it is obvious that my server (Apache), along with PHP, definitely sent the UTF-8 string back.

Another hint is that you can try converting your strings to HTML objects . For example, echo "&#x00A9;"; will print © . But I'm not 100% sure how to convert all strings to HTML objects using PHP. I tried unsuccessfully with htmlentities() and htmlspecialchars() , but that didn't change anything for IE.

+3
source

I have the same problem. But I can handle it.

convert the encoding "echo.php" 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 :)

+2
source

This happens when the charset is undefined or incorrect.

You can use meta tags to define encodings. Place the following meta tags as necessary in the page title section, and it will display correctly.

HTML 4

 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

HTML 5

 <meta charset="utf-8" /> 
+1
source

add meta tags to title

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> 
0
source

MySQL and PHP must process the same character set.

Try to add ..

mysqli_set_charset("utf8");

.. after connecting to the database. Always works for me!

0
source

If you use php and mssql, you also need to specify the character set 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); 

Note that you still need to do what was suggested above, declare your html encoding, like this HTML 5:

 <meta charset="utf-8" /> 

php charset:

 header('content-type: text/html; charset=utf-8'); 
0
source

All Articles