PHP uses mb_check_encoding to check whether a string is valid in the specified encoding_PHP tutorial

WBOY
Release: 2016-07-13 10:25:25
Original
932 people have browsed it

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)
Description: 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 can effectively avoid the so-called "Invalid Encoding Attack".
Parameters
var
The byte stream to check. If this parameter is omitted, this function checks all input from the original request.
encoding
The desired encoding.
Return Value
Returns TRUE on success, or FALSE on failure.
In order to check if a string is correctly encoded in UTF-8, I suggest the following function to implement mb_check_encoding(): 🎜>

function check_utf8($str) { $len = strlen($str); for($i = 0; $i < $len ; $i++){ $c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false;
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($ i+$ bytes) & gt; $ len) Return false;
While ($ bytes & gt; 1) {
$ i ++; $str[$i]);
if ($b < 128 || $b > 191) return false;
$bytes--;
}
}
}
return true;
} // end of check_utf8
?>





http://www.bkjia.com/PHPjc/825083.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/825083.html

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) Description: bool mb_check_encoding ([ string $var = NULL [, st...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!