Search code examples
phputf-8

How to best configure PHP to handle a UTF-8 website


What extensions would you recommend and how should php be best configured to create a website that uses utf-8 encoding for everything. eg...

  • Page output is utf-8
  • forms submit data encoded in utf-8
  • internal processing of string data (eg when talking to a database) are all in utf-8 as well.

It seems that php does not really cope well with multibyte character sets at the moment. So far I have worked out that mbstring looks like an important extension.

Is it worth the hassle..?


Solution

  • The supposed issues of PHP with Unicode content have been somewhat overstated. I've been doing multilingual websites since 1998 and never knew there might be an issue until I've read about it somewhere - many years and websites later.

    This works just fine for me:

    Apache configuration (in httpd.conf or .htaccess)

    AddDefaultCharset utf-8
    

    PHP (in php.ini)

    default_charset = "utf-8"
    mbstring.internal_encoding=utf-8
    mbstring.http_output=UTF-8
    mbstring.encoding_translation=On
    mbstring.func_overload=6 
    

    MySQL

    CREATE your database with an utf8_* collation, let the tables inherit the database collation and start every connection with "SET NAMES utf8"

    HTML (in HEAD element)

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