Mb_check_encoding-check whether the string is valid in the specified encoding.
PHP version requirements: (PHP 4> = 4.4.3, PHP 5> = 5.1.3)
Bo: bool mb_check_encoding ([string $ var = NULL [, string $ encoding = mb_internal_encoding ()])
Check whether the specified byte stream is valid in the specified encoding. It effectively avoids the so-called "Invalid Encoding Attack )".
Parameters
Var
The byte stream to be checked. If this parameter is omitted, this function checks all input from the initial request.
Encoding
The expected encoding.
Return Value
Returns TRUE if the call succeeds, or FALSE if the call fails.
To check whether a string is correctly encoded in utf-8, we recommend the following function to implement mb_check_encoding ():
Copy codeThe Code is as follows:
<? Php
Function check_utf8 ($ str ){
$ Len = strlen ($ str );
For ($ I = 0; $ I <$ len; $ I ++ ){
$ C = ord ($ str [$ I]);
If ($ c & gt; 128 ){
If ($ c> 247) return false;
Elseif ($ c & gt; 239) $ bytes = 4;
Elseif ($ c> 223) $ bytes = 3;
Elseif ($ c> 191) $ bytes = 2;
Else return false;
If ($ I + $ bytes)> $ len) return false;
While ($ bytes> 1 ){
$ I ++;
$ B = ord ($ str [$ I]);
If ($ B <128 | $ B> 191) return false;
$ Bytes --;
}
}
}
Return true;
} // End of check_utf8
?>