PHP, Unicode and UTF-8 without hassles

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);
comments powered by Disqus