note 99813 added to function.mb-check-encoding
| From: | php at linuxhosted dot ca | Date: | Wed, 08 Sep 2010 17:23:00 +0000 |
| Subject: | note 99813 added to function.mb-check-encoding | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172379@lists.php.net to get a copy of this message | ||
Here is the ultimate UTF-8 validity check function, it is by far the fastest function of any others
listed here, and it is one of the only ones that actually pass all validity checks, and does not
crash php or hit the recursion limit for PCRE.
On an error it will return NULL, otherwise true if $string is valid UTF-8, otherwise false.
The common w3 PCRE regular expression match from:
http://w3.org/International/questions/qa-forms-utf-8.html
does not match all valid UTF-8 in the 0-127 ASCII range, and will also cause a recursion limit or
PHP segfault if the input string is too long.
<?php
function is_utf8($string) {
if (!is_string($string))
return NULL;
$valid = preg_match('##Dsu', $string);
if ($valid === false) {
$error = preg_last_error();
switch ($error) {
case PREG_BAD_UTF8_ERROR:
case PREG_BAD_UTF8_OFFSET_ERROR:
return false;
default:
return NULL;
}
}
return (bool)$valid;
}
?>
I made a testing script to check the various functions for testing UTF-8 validity for speed and
accuracy, the modified w3 preg_match is simply fixed to allow all ascii characters.
For the full code of my UTF-8 function testing script, see:
http://svn.sctbdev.ca/wsvn/tests/utf8.php
Results from my testing script:
Building string of all possible valid UTF-8 characters ... DONE in 1.72670s (4382592 bytes)
Building random data string of same length ............... DONE in 1.38229s (4382592 bytes)
------------------------------------------------------------------
is_utf8():
Passed validation of all UTF-8 conditions.
Testing 64 iterations with Random Data ... PASSED after 0.00005s
Testing 64 iterations with Valid Data .... PASSED after 0.51578s
------------------------------------------------------------------
mb_check_encoding():
Valid 4 Byte Sequence (but above Unicode codepoints): FAILED
Valid 5 Byte Sequence (but above Unicode codepoints): FAILED
Valid 6 Byte Sequence (but above Unicode codepoints): FAILED
Failed to validate all UTF-8 conditions correctly.
Testing 64 iterations with Random Data ... PASSED after 3.53275s
Testing 64 iterations with Valid Data .... PASSED after 3.15102s
------------------------------------------------------------------
mb_detect_encoding():
Valid 2 Byte Sequence (but contains Overlong): FAILED
Valid 3 Byte Sequence (but contains Overlong): FAILED
Valid 4 Byte Sequence (but contains Overlong): FAILED
Valid 5 Byte Sequence (but contains Overlong): FAILED
Valid 6 Byte Sequence (but contains Overlong): FAILED
Valid 3 Byte Sequence (but contains Surrogate): FAILED
Valid 4 Byte Sequence (but above Unicode codepoints): FAILED
Valid 5 Byte Sequence (but above Unicode codepoints): FAILED
Valid 6 Byte Sequence (but above Unicode codepoints): FAILED
Failed to validate all UTF-8 conditions correctly.
Testing 64 iterations with Random Data ... PASSED after 1.40612s
Testing 64 iterations with Valid Data .... PASSED after 3.09515s
------------------------------------------------------------------
mb_convert_encoding():
Passed validation of all UTF-8 conditions.
Testing 64 iterations with Random Data ... PASSED after 6.20198s
Testing 64 iterations with Valid Data .... PASSED after 5.36951s
------------------------------------------------------------------
w3 preg_match():
Valid 1 Byte Sequence: FAILED
Failed to validate all UTF-8 conditions correctly.
Testing 64 iterations with Random Data ... PASSED after 0.00014s
Testing 64 iterations with Valid Data .... FAILED after 0.00014s
------------------------------------------------------------------
modified w3 preg_match():
Passed validation of all UTF-8 conditions.
Testing 64 iterations with Random Data ... PASSED after 0.00014s
Testing 64 iterations with Valid Data .... FAILED after 0.05054s
------------------------------------------------------------------
----
Server IP: 91.199.167.223
Probable Submitter: 85.17.93.178
----
Manual Page -- http://www.php.net/manual/en/function.mb-check-encoding.php
Edit -- https://master.php.net/note/edit/99813
Del: integrated -- https://master.php.net/note/delete/99813/integrated
Del: useless -- https://master.php.net/note/delete/99813/useless
Del: bad code -- https://master.php.net/note/delete/99813/bad+code
Del: spam -- https://master.php.net/note/delete/99813/spam
Del: non-english -- https://master.php.net/note/delete/99813/non-english
Del: in docs -- https://master.php.net/note/delete/99813/in+docs
Del: other reasons-- https://master.php.net/note/delete/99813
Reject -- https://master.php.net/note/reject/99813
Search -- https://master.php.net/manage/user-notes.php