Php – Switch website encoding from ISO-8859-1 to UTF-8

encodingiso-8859-1PHPutf-8

I am trying to convert my existing PHP webpage to use UTF-8 encoding.

To do so, I have done the following things:

  1. specified UTF-8 as the charset in the meta content tag at the start of my webpage.
  2. change the default_charset to UTF-8 in the php.ini.
  3. specified UTF-8 as the iconv encoding in the php.ini file.
  4. specified UTF-8 in my .htaccess file using: AddDefaultCharset UTF-8.

Yet after all that, when i echo mb_internal_encoding(), it shows as ISO-8859-1. What am I missing here? I know I could use auto_prepend to attach a script that changes the default encoding to UTF-8, but I'm just trying to understand what I'm missing.

Thanks

Best Answer

mb_internal_encoding() doesn't effect the output of your scripts per se, it effects the default encoding when using the multibyte string functions and the conversion of POST and GET inputs.

Simply set with

mbstring.internal_encoding='UTF-8'

in your php.ini file, or programmatically with:

mb_internal_encoding('UTF-8');

Speaking of the mb_ functions, you'll need to rewrite your scripts to use these, e.g. mb_strlen() instead of strlen.(), etc.

Also check what HTTP content-type headers are being outputted, though from what you've done it should be ok.

If you using a database, you'll also have to convert that too, and specify that you're using UTF-8 when connecting to it.