note 99982 added to function.utf8-encode
| From: | Oneofyou at osu1 dot php dot net | Date: | Fri, 17 Sep 2010 19:49:26 +0000 |
| Subject: | note 99982 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172699@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:
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
----
Server IP: 69.147.83.197
Probable Submitter: 88.214.195.254
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- https://master.php.net/note/edit/99982
Del: integrated -- https://master.php.net/note/delete/99982/integrated
Del: useless -- https://master.php.net/note/delete/99982/useless
Del: bad code -- https://master.php.net/note/delete/99982/bad+code
Del: spam -- https://master.php.net/note/delete/99982/spam
Del: non-english -- https://master.php.net/note/delete/99982/non-english
Del: in docs -- https://master.php.net/note/delete/99982/in+docs
Del: other reasons-- https://master.php.net/note/delete/99982
Reject -- https://master.php.net/note/reject/99982
Search -- https://master.php.net/manage/user-notes.php