note 68607 added to function.mb-detect-encoding
| From: | chrisATw3style dot coDOTuk at osu1 dot php dot net | Date: | Thu, 03 Aug 2006 09:22:16 +0000 |
| Subject: | note 68607 added to function.mb-detect-encoding | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115508@lists.php.net to get a copy of this message | ||
Based upon that snippet below using preg_match() I needed something faster and less specific. That
function works and is brilliant but it scans the entire strings and checks that it conforms to
UTF-8. I wanted something purely to check if a string contains UTF-8 characters so that I could
switch character encoding from iso-8859-1 to utf-8.
I modified the pattern to only look for non-ascii multibyte sequences in the UTF-8 range and also to
stop once it finds at least one multibytes string. This is quite a lot faster.
<?php
function detectUTF8($string)
{
return preg_match('%(?:
[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)+%xs', $string);
}
?>
----
Server IP: 85.116.0.130
Probable Submitter: 213.205.138.154
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.mb-detect-encoding.php
Edit -- https://master.php.net/note/edit/68607
Del: integrated -- https://master.php.net/note/delete/68607/integrated
Del: useless -- https://master.php.net/note/delete/68607/useless
Del: bad code -- https://master.php.net/note/delete/68607/bad+code
Del: spam -- https://master.php.net/note/delete/68607/spam
Del: non-english -- https://master.php.net/note/delete/68607/non-english
Del: in docs -- https://master.php.net/note/delete/68607/in+docs
Del: other reasons-- https://master.php.net/note/delete/68607
Reject -- https://master.php.net/note/reject/68607
Search -- https://master.php.net/manage/user-notes.php