note 99982 modified in function.utf8-encode by danbrown
| From: | danbrown@php.net | Date: | Fri, 17 Sep 2010 19:52:33 +0000 |
| Subject: | note 99982 modified in function.utf8-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172700@lists.php.net to get a copy of this message | ||
Reason: Mysql prior 5.5 very strict in parsing UTF-8, it does not understand 4-byte and more UTF-8
chars and truncates such strings.
Solution:
<?php
function get_correct_utf8_mysql_string($s)
{
if(empty($s)) return $s;
$s = preg_match_all("#[\x09\x0A\x0D\x20-\x7E]|
[\xC2-\xDF][\x80-\xBF]|
\xE0[\xA0-\xBF][\x80-\xBF]|
[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|
\xED[\x80-\x9F][\x80-\xBF]#x", $s, $m );
return implode("",$m[0]);
}
?>
Function cleans string from everything except correct ASCII & UTF-8 characters (excluding
4-byte+ UTF-8 sequences).
Ready to insert into utf-8 mysql database.
Fast, clean and easy to understand.You can easily convert it to 4-byte sequences using example
below.
Disadvantage: it can make mess from incorrect UTF-8 string, but this mess will be valid UTF-8
--was--
Reason: Mysql prior 5.5 very strict in parsing UTF-8, it does not understand 4-byte and more UTF-8
chars and truncates such strings.
Solution:
function get_correct_utf8_mysql_string($s)
{
if(empty($s)) return $s;
$s = preg_match_all("#[\x09\x0A\x0D\x20-\x7E]|
[\xC2-\xDF][\x80-\xBF]|
\xE0[\xA0-\xBF][\x80-\xBF]|
[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|
\xED[\x80-\x9F][\x80-\xBF]#x", $s, $m );
return implode("",$m[0]);
}
Function cleans string from everything except correct ASCII & UTF-8 characters (excluding
4-byte+ UTF-8 sequences).
Ready to insert into utf-8 mysql database.
Fast, clean and easy to understand.You can easily convert it to 4-byte sequences using example
below.
Disadvantage: it can make mess from incorrect UTF-8 string, but this mess will be valid UTF-8
http://php.net/manual/en/function.utf8-encode.php