I am working for international clients who have all very different alphabets and so I am trying to finally get an overview of a complete workflow between PHP and MySQL that would ensure all character encodings to be inserted correctly. I have read a bunch of tutorials on this but still have questions(there is much to learn) and thought I might just put it all together here and ask.
PHP
header('Content-Type:text/html; charset=UTF-8');
mb_internal_encoding('UTF-8');
HTML
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<form accept-charset="UTF-8"> .. </form>
(though the later is optional and rather a suggestion but I belief I'd rather suggest as not doing anything)
MySQL
CREATE database_name DEFAULT CHARACTER SET utf8;
or ALTER database_name DEFAULT CHARACTER SET utf8;
and/or use utf8_general_ci
as MySQL connection collation.
(it is important to note here that this will increase the database size if it uses varchar)
Connection
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET utf8");
Businesses logic
detect if not UTF8 with mb_detect_encoding()
and convert with ivon()
.
validating overly long sequences of UTF8 and UTF16
$body=preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]|(?<=^|[\x00-\x7F])[\x80-\xBF]+|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/','�',$body);
$body=preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $body);
Questions
is
mb_internal_encoding('UTF-8')
necessary in PHP 5.3 and higher and if so does this mean I have to use all multi byte functions instead of its core functions likemb_substr()
instead ofsubstr()
?is it still necessary to check for malformed input stings and if so what is a reliable function/class to do so? I possibly do not want to strip bad data and don't know enough about transliteration.
should it really be
utf8_general_ci
or ratherutf8_bin
?is there something missing in the above workflow?
sources:
http://coding.smashingmagazine.com/2012/06/06/all-about-unicode-utf8-character-sets/
http://webcollab.sourceforge.net/unicode.html
http://stackoverflow.com/a/3742879/1043231
http://www.adayinthelifeof.nl/2010/12/04/about-using-utf-8-fields-in-mysql/
http://akrabat.com/php/utf8-php-and-mysql/