问题
I'm trying to place some Chinese text on a website, but as soon as the page is placed online, instead of Chinese text, i see a row of question marks ?????????? ???????????
I tested the same page on a WAMP server before putting it online (all the pages have a php extension) and the Chinese characters show just fine, it is only when the pages are requested from the online host server do i see all the question marks.
the page contains (if this helps):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
回答1:
If you're using a database of some sort, make sure you run this before querying the database (but after making the connection):
mysql_set_charset("utf8");
回答2:
The actual HTTP response headers will always override the HTML <meta>
headers. As per your comment:
I'm not too familiar with headers, here is the response header from the working page:
Date: Thu, 24 Jun 2010 05:24:23 GMT Server: Apache/2.0.63 (Win32) PHP/5.2.11 X-Powered-By: PHP/5.2.11 Content-Length: 3622 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8 200 OKand the non-working page:Date: Thu, 24 Jun 2010 05:26:54 GMT Server: Apache X-Powered-By: PHP/5.2.12 Connection: close Transfer-Encoding: chunked Content-Type: text/html 200 OK
See, the HTTP response header of the non-working page has a Content-Type
of text/html
without any specified charset. It should have been text/html; charset=UTF-8
.
You can go around this by adding the following line to the top of your PHP page, before you emit any character (HTML) to the response body.
header('Content-Type: text/html; charset=UTF-8');
Update: as per the comments, the above cause seems to be excluded. Next check; is the file itself saved as UTF-8? Rightclick page and view source. Are the question marks also there? If so, then something went wrong during FTP transfer to the hosting. Choose binary instead of text/ASCII or set character encoding for transferred text files in FTP client settings and retry.
回答3:
I had a similar problem and was able to resolve it.
I had the content of the site passing through PHP. When I viewed it as a raw HTML file, it would display the characters properly, but if I passed it through PHP (in my case, by using auto_prepend_file), it would turn the characters into question marks.
The solution was to turn OFF detect_unicode in the PHP settings. In my case, I just edited the .htaccess file to include the line:
php_value detect_unicode "0"
...and the Chinese characters began displaying properly. There might be an equivalent solution using ini_set().
Hope this helps.
回答4:
Since the mysql_* functions in PHP are deprecated as of PHP 5.5.0, here's the SQL that you can execute that essentially does the same as mysql_set_charset:
SQL
SET character_set_client = utf8
This was key in getting my forms to save unicode text to MySQL. Execute that SQL query immediately after establishing a connection to your MySQL server.
来源:https://stackoverflow.com/questions/3107033/question-marks-instead-of-chinese-characters