note 29662 deleted from function.utf8-decode by cece

From: Date: Mon, 18 Aug 2003 21:15:58 +0000
Subject: note 29662 deleted from function.utf8-decode by cece
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54616@lists.php.net to get a copy of this message
Note Submitter: utha@free.fr ---- Well sometimes you need to store a utf-8 string in a database table column with a fixed size. Here is a function that fix a string which has been broken (by a substr for example) in a middle of a utf-8 char sequence. function FixUtf8BrokenString($Desc) { // UTF-8 encoding // bytes : representation // 1 : 0bbbbbbb // 2 : 110bbbbb 10bbbbbb // 3 : 1110bbbb 10bbbbbb 10bbbbbb // 4 : 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb // to see if a string is broken in middle of a utf8 char // we search for last byte encoding size of utf-8 char // if number of last bytes in string is lower than encoding size // we remove those last bytes // if last byte is ord < 128, ok. we return ! if (ord($Desc[strlen($Desc) - 1]) < (0x80)) return $Desc // loop for finding byte encoding size $nbbytes = 1; while (ord($Desc[strlen($Desc) - $nbbytes]) > 0x7F) { if (ord($Desc[strlen($Desc) - $nbbytes]) > 0xBF) break; $nbbytes++; } // check if byte encoding size is encoding a size of 4 bytes if (($Desc[strlen($Desc) - $nbbytes] & 0xF8 == 0xF0) && ($nbbytes == 4) return $Desc; // check if byte encoding size is encoding a size of 3 bytes if (($Desc[strlen($Desc) - $nbbytes] & 0xF0 == 0xE0) && ($nbbytes == 3) return $Desc; // check if byte encoding size is encoding a size of 2 bytes if (($Desc[strlen($Desc) - $nbbytes] & 0xE0 == 0xC0) && ($nbbytes == 2) return $Desc; // then this is the case where string is badly broken, we remove last bytes return substr($Desc, 0, -$nbbytes); } $str = "Ekonomi_ve_\xc4\xb0\xc5\x9f_D\xc3\xbcnyas\xc4\xb1"; $broken = substr($str, 0, 12); $fixed = FixUtf8BrokenString($broken); echo "$str<hr>$fixed<hr>$broken<hr>"; Sebastien Meudec.

« previous php.notes (#54616) next »