4

I'm trying to display Japanese characters on a PHP page. No loading from the database, just stored in a language file and echo'ed out.

I'm running into a weird scenario. I have the page properly setup with UTF-8 and I test a sample page on my local WAMP server and it works.

The moment I tested it out our development and production servers the characters don't display properly.

This leads me to believe then that it's a setting in php.ini. But I haven't found much information about this so I'm not really sure if this is the issue.

Is there something fundamental I'm missing?

Thanks

Makoto
  • 104,088
  • 27
  • 192
  • 230
AndreLiem
  • 2,031
  • 5
  • 20
  • 26

4 Answers4

6

You have to deliver the documents with the proper encoding declaration in the HTTP header field Content-Type.

In PHP you do this via the header function before the first data has been send to the client, so preferably as one of the first statements:

<?php
    header('Content-Type: text/html;charset=utf-8');

    // the rest
Gumbo
  • 643,351
  • 109
  • 780
  • 844
6

Since you've stated that it is working in your development environment and not in your live, you might want to check Apache's AddDefaultCharset and set this to UTF-8, if it's not already.

I tend to make sure the following steps are checked

  1. PHP Header is sent in UTF-8
  2. Meta tag is set to UTF-8 (Content-Type)
  3. Storage is set to UTF-8
  4. Server output is set to UTF-8

That seems to work for me. Hope this helps.

Kieran Hall
  • 2,637
  • 2
  • 24
  • 27
  • Thanks, I'll have to test it out but that looks to be everything I need. – AndreLiem Mar 20 '09 at 17:26
  • I have done the following adding the header to the top of the php file header('Content-type: text/html; charset=utf-8');. I have then proceeded to set up the meta tag as and I checked my database collation which is set to utf8_general_ci. It is still showing the japanese characters as ?. Am I missing something? – g4ost Sep 13 '19 at 15:27
1

Firstly, I'll assume the same client machine is used for both tests.

So, use Firebug or your tool-of-choice to check the HTTP response headers on your local server, and compare them with the headers generated by the other servers. You will no doubt find a difference.

Typically your server should be including a header like this in the response:

Content-Type: text/html; charset=UTF-8

If the headers on the two systems look pretty much the same, grab the body of both responses and load it up in a hex editor and look for encoding differences.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
0

Try following (worked for me, CentOS 6.8, PHP 5.6)

#1
Apache Config

/etc/httpd/conf/httpd.conf
AddDefaultCharset UTF-8

#2
PHP Config

/etc/php.ini:
default_charset = "utf-8" >> default_charset = "Shift_JIS"

Note : set error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

#3
html head meta

http-equiv="content-type" content="text/html; charset=Shift_JIS"

MD TAREQ HASSAN
  • 1,188
  • 20
  • 46