Edit

PHP, Unicode and UTF-8 without hassles

Alt text

Enable mbstring in PHP

PHP doesn’t natively support unicode but it’s compatible, you can check if multibytes functions are available.

php -m | grep mbstring

You can enable it with sudo apt-get install php7.0-mbstring or at compile time by adding this flag --enable-mbstring. More info here. If you can’t install mbstring via those ways, you can use the polyfill provided by symfony.

composer require symfony/polyfill-mbstring

PHP multibyte functions are documented here mb_*

UTF-8 and PHP

php -r "echo ini_get('default_charset');"

If you use PHP EXIF extension you can also check php -r "echo ini_get('exif.encode_unicode');"

Make you application UTF-8 ready !

HTML
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
HTTP
Content-Type: text/html; charset=utf-8
PHP PDO
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$dbh->exec("SET CHARACTER SET utf8");

OR

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);

Enforce UTF-8

composer require patchwork/utf8

\Patchwork\Utf8\Bootup::initAll();
\Patchwork\Utf8\Bootup::filterRequestUri();
\Patchwork\Utf8\Bootup::filterRequestInputs();
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');

Never use utf8_decode() and utf8_encode() in your application.

They are misspelled, they only encode and decode between ISO-8859-1 and UTF-8 think about iso88591_to_utf8 for utf8_decode and utf8_to_iso88591 for utf8_encode .

If you want convert to another charset (it’s called transliteration) you can transliterate with iconv

echo iconv('UTF-8', 'ASCII//TRANSLIT', 'text');
$string = '€';
echo iconv(mb_detect_encoding($string), 'ASCII//TRANSLIT', $string);
%23%23%20PHP%2C%20Unicode%20and%20UTF-8%20without%20hassles%0A%0A@%28Postach.io%29%5Butf-8%2C%20php%2C%20published%5D%0A%0A%21%5BAlt%20text%5D%28./php-unicode.png%29%0A%0A%23%23%23%20Enable%20mbstring%20in%20PHP%0APHP%20**doesn%27t%20natively%20support**%20unicode%20but%20it%27s%20**compatible**%2C%20you%20can%20check%20if%20multibytes%20functions%20are%20available.%0A%0A%60%60%60cmd%0Aphp%20-m%20%7C%20grep%20mbstring%0A%60%60%60%0AYou%20can%20enable%20it%20with%20%60sudo%20apt-get%20install%20php7.0-mbstring%60%20or%20at%20compile%20time%20by%20adding%20this%20flag%20%60--enable-mbstring%60.%20More%20info%20%5Bhere%5D%28http%3A//php.net/manual/en/mbstring.installation.php%29.%20If%20you%20can%27t%20install%20mbstring%20via%20those%20ways%2C%20you%20can%20use%20the%20**polyfill**%20provided%20by%20symfony.%20%0A%0A%3E%20**composer%20require%20symfony/polyfill-mbstring**%0A%0APHP%20multibyte%20functions%20are%20documented%20here%20%5Bmb_*%5D%28http%3A//php.net/manual/en/book.mbstring.php%29%0A%0A%23%23%23%20UTF-8%20and%20PHP%0A%0A%60%60%60%0Aphp%20-r%20%22echo%20ini_get%28%27default_charset%27%29%3B%22%20%0A%60%60%60%0A%0A%3E%20If%20you%20use%20**PHP%20EXIF**%20extension%20you%20can%20also%20check%20%60php%20-r%20%22echo%20ini_get%28%27exif.encode_unicode%27%29%3B%22%20%60%20%0A%0A%0A%23%23%23%20Make%20you%20application%20UTF-8%20ready%20%21%0A%0A%23%23%23%23%23%20HTML%0A%60%60%60html%0A%3Cmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text/html%3B%20charset%3Dutf-8%22%20/%3E%0A%60%60%60%0A%0A%23%23%23%23%23%20HTTP%0A%60%60%60http%0AContent-Type%3A%20text/html%3B%20charset%3Dutf-8%0A%60%60%60%0A%0A%23%23%23%23%23%20PHP%20PDO%0A%60%60%60php%0A%24dbh%20%3D%20new%20PDO%28%27mysql%3Ahost%3Dlocalhost%3Bdbname%3Dtest%27%2C%20%24user%2C%20%24pass%29%3B%0A%24dbh-%3Eexec%28%22SET%20CHARACTER%20SET%20utf8%22%29%3B%0A%60%60%60%0A**OR**%0A%60%60%60php%0A%24dbh%20%3D%20new%20PDO%28%27mysql%3Ahost%3Dlocalhost%3Bdbname%3Dtest%27%2C%20%24user%2C%20%24pass%2C%20array%28%0A%09PDO%3A%3AMYSQL_ATTR_INIT_COMMAND%20%3D%3E%20%22SET%20NAMES%20utf8%22%29%0A%29%3B%0A%60%60%60%0A%0A**Enforce%20UTF-8**%0A%0A%3E%20composer%20require%20%60patchwork/utf8%60%20%0A%0A%60%60%60php%0A%5CPatchwork%5CUtf8%5CBootup%3A%3AinitAll%28%29%3B%0A%5CPatchwork%5CUtf8%5CBootup%3A%3AfilterRequestUri%28%29%3B%0A%5CPatchwork%5CUtf8%5CBootup%3A%3AfilterRequestInputs%28%29%3B%0Amb_internal_encoding%28%27UTF-8%27%29%3B%0Amb_http_output%28%27UTF-8%27%29%3B%0A%60%60%60%0A%0A%3E%20%20**Never%20use**%20%5Butf8_decode%28%29%5D%28http%3A//php.net/manual/fr/function.utf8-decode.php%29%20and%20%5Butf8_encode%28%29%5D%28http%3A//php.net/manual/fr/function.utf8-encode.php%29%20in%20your%20application.%0A%0AThey%20are%20misspelled%2C%20they%20only%20encode%20and%20decode%20between%20%60ISO-8859-1%60%20and%20%60UTF-8%60%20think%20about%20%60iso88591_to_utf8%60%20for%20%60utf8_decode%60%20and%20%60utf8_to_iso88591%60%20for%20%60utf8_encode%60%20.%0A%0AIf%20you%20want%20convert%20to%20another%20charset%20%28it%27s%20called%20**transliteration**%29%20you%20can%20transliterate%20with%20%5Biconv%5D%28http%3A//php.net/manual/fr/function.iconv.php%29%0A%0A%60%60%60%0Aecho%20iconv%28%27UTF-8%27%2C%20%27ASCII//TRANSLIT%27%2C%20%27text%27%29%3B%0A%0A%24string%20%3D%20%27%u20AC%27%3B%0Aecho%20iconv%28mb_detect_encoding%28%24string%29%2C%20%27ASCII//TRANSLIT%27%2C%20%24string%29%3B%0A%60%60%60%20%0A%0A