Mb_check_encoding
- (PHP 4 >= 4.4.3, PHP 5 >= 5.1.3, PHP 7)
- Mb_check_encoding-check if the string is valid for the specified encoding
- mb_check_encoding-checks if the string is valid in the specified encoding
Description
mb_check_encoding([$varNULL[,$encodingmb_internal_encoding()]])// Checks if the specified byte stream is valid for the specified encoding. // It is useful to prevent so-called "Invalid Encoding Attack".// 检查指定的字节流在指定的编码里是否有效。它能有效避免所谓的“无效编码攻击(Invalid Encoding Attack)”。
Parametersvar
- The byte stream to check. If It is a omitted, this function checks all the input from the beginning of the request.
- The byte stream to check. If this argument is omitted, this function checks all input from the original request.
Encoding
- The expected encoding.
- the desired encoding.
Return Values
- Returns TRUE on success or FALSE on failure.
- Returns TRUE on success, or FALSE on failure.
Examples
<?php/*** Created by Phpstorm.* User:zhangrongxiang* DATE:2018/1/27* Time: PM 2:59 *//** Pure Digital and English alphabet combinations */$utf 8Str="I had 4 books and 2 magazines to check out.";Echo ( mb_check_encoding( $utf 8Str, ' Utf-8 ' ) ).Php_eol; //Output 1Echo ( mb_check_encoding( $utf 8Str, ' GBK ' ) ).Php_eol; //Output 1Echo Bin2Hex( $utf 8Str ).Php_eol;//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20$gbkStr=mb_convert_encoding( $utf 8Str, ' GBK ', ' Utf-8 ' );Echo Bin2Hex( $gbkStr ).Php_eol;//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20/**GBK encoded string-to-set file encoded as gbk*/$str=' Blog Park and GitHub. ';Echo mb_check_encoding( $str, ' Utf-8 ' ).Php_eol; //Output emptyEcho mb_check_encoding( $str, ' GBK ' ).Php_eol; //Output 1/**utf-8 encoded string-to-set file encoded as utf-8*/$str=' Blog Park and GitHub. ';Echo mb_check_encoding( $str, ' Utf-8 ' ).Php_eol; //1Echo mb_check_encoding( $str, ' GBK ' ).Php_eol; //Output empty$utf 8Str=' Who's my ABC? ';Echo mb_check_encoding( $utf 8Str, ' Utf-8 ' ).Php_eol; //Output 1//If there is a Chinese punctuation mark is empty!!! Echo mb_check_encoding( $utf 8Str, ' GBK ' ).Php_eol; //Output 1/** Custom Detect if string encoding is utf-8*/functionIs_utf8( $str ){return (BOOL) Preg_match( '//u ', Serialize($str) );}Echo ' Hello China! '. Is_utf8( ' Hello China! ' ).Php_eol; //1functionCheck_utf8( $str ){$len=strlen( $str ); for ( $i=0; $i<$len; $i++){$c=Ord( $str[ $i ] ); if ( $c> - ){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 )>$len ){return false;} while ( $bytes>1 ){$i++; $b=Ord( $str[ $i ] ); if ( $b< -||$b>191 ){return false;}$bytes--;} } }return true;}//End of Check_utf8EchoCheck_utf8("Hello China").Php_eol; //1EchoCheck_utf8( "\x00\xe3").Php_eol; //Empty/** Check a strings encoded value * /functionCheckencoding( $string, $string _encoding ){$fs=$string _encoding==' UTF-8 ' ? ' UTF-32 ' : $string _encoding; $ts=$string _encoding==' UTF-32 ' ? ' UTF-8 ' : $string _encoding; return $string===mb_convert_encoding( mb_convert_encoding( $string, $fs, $ts ), $ts, $fs );}/ * Test 1 variables * /$string="\x00\x81";$encoding="Shift_JIS";/* Test 1 mb_check_encoding (test for bad byte stream) */if ( true===mb_check_encoding( $string, $encoding ) ){Echo ' Valid ('.$encoding.') encoded byte stream! '.Php_eol;}Else{Echo ' Invalid ('.$encoding.') encoded byte stream! '.Php_eol;}/* Test 1 checkencoding (test for bad byte sequence (s)) * /if ( true= = = Checkencoding( $string, $encoding ) ){Echo ' Valid ('.$encoding.') encoded byte sequence! '.Php_eol;}Else{Echo ' Invalid ('.$encoding.') encoded byte sequence! '.Php_eol;}/ * Test 2 * // * Test 2 variables * /$string="\x00\xe3";$encoding="UTF-8";/* Test 2 mb_check_encoding (test for bad byte stream) */if ( true===mb_check_encoding( $string, $encoding ) ){Echo ' Valid ('.$encoding.') encoded byte stream! '.Php_eol;}Else{Echo ' Invalid ('.$encoding.') encoded byte stream! '.Php_eol;}/* Test 2 checkencoding (test for bad byte sequence (s)) * /if ( true= = = Checkencoding( $string, $encoding ) ){Echo ' Valid ('.$encoding.') encoded byte sequence! '.Php_eol;}Else{Echo ' Invalid ('.$encoding.') encoded byte sequence! '.Php_eol;}
Article reference
- http://php.net/manual/zh/function.mb-check-encoding.php
Reprint Annotated Source
PHP mb_check_encoding Use