php stream encoding,PHP之mb_check_encoding使用方法分享
本文主要和大家分享mb_check_encoding使用方法,希望能幫助到大家。
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 — 檢查字符串在指定的編碼里是否有效
Descriptionbool mb_check_encoding ([ string $var = NULL [, string $encoding = mb_internal_encoding() ]] )
// Checks if the specified byte stream is valid for the specified encoding.
// It is useful to prevent so-called "Invalid Encoding Attack".
// 檢查指定的字節(jié)流在指定的編碼里是否有效。它能有效避免所謂的“無(wú)效編碼攻擊(Invalid Encoding Attack)”。
Parameters
varThe byte stream to check. If it is omitted, this function checks all the input from the beginning of the request.
要檢查的字節(jié)流。如果省略了這個(gè)參數(shù),此函數(shù)會(huì)檢查所有來(lái)自最初請(qǐng)求所有的輸入。
encodingThe expected encoding.
期望的編碼。
Return ValuesReturns TRUE on success or FALSE on failure.
成功時(shí)返回 TRUE, 或者在失敗時(shí)返回 FALSE。
Examples<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/1/27
* Time: 下午2:59
*/
/**純數(shù)字和英文字母組合*/
$utf8Str = "I have 4 books and 2 magazines to check out. ";
echo ( mb_check_encoding( $utf8Str, 'utf-8' ) ) . PHP_EOL; //輸出1
echo ( mb_check_encoding( $utf8Str, 'gbk' ) ) . PHP_EOL; //輸出1
echo bin2hex( $utf8Str ) . PHP_EOL;
//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20
$gbkStr = mb_convert_encoding( $utf8Str, 'gbk', 'utf-8' );
echo bin2hex( $gbkStr ) . PHP_EOL;
//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20
/**gbk編碼的字符串 --> 設(shè)置文件編碼為gbk*/
$str = '博客園和github。';
echo mb_check_encoding( $str, 'utf-8' ) . PHP_EOL; //輸出空
echo mb_check_encoding( $str, 'gbk' ) . PHP_EOL; //輸出1
/**utf-8編碼的字符串 --> 設(shè)置文件編碼為utf-8*/
$str = '博客園和github。';
echo mb_check_encoding( $str, 'utf-8' ) . PHP_EOL; //1
echo mb_check_encoding( $str, 'gbk' ) . PHP_EOL; //輸出空
$utf8Str = '我abc是誰(shuí).';
echo mb_check_encoding( $utf8Str, 'utf-8' ) . PHP_EOL; //輸出1
//如果有中文標(biāo)點(diǎn)符號(hào)則為空!!!
echo mb_check_encoding( $utf8Str, 'gbk' ) . PHP_EOL; //輸出1
/**自定義檢測(cè)字符串編碼是否為utf-8*/
function is_utf8( $str ) {
return (bool) preg_match( '//u', serialize($str) );
}
echo 'hello 中國(guó)!' .is_utf8( 'hello 中國(guó)!' ) . PHP_EOL; //1
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 ) > $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
echo check_utf8("hello 中國(guó)").PHP_EOL; // 1
echo check_utf8( "\x00\xE3").PHP_EOL; //空
/** check a strings encoded value */
function checkEncoding( $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;
}
相關(guān)推薦:
總結(jié)
以上是生活随笔為你收集整理的php stream encoding,PHP之mb_check_encoding使用方法分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机培训开场语,辅导班家长会主持词开场
- 下一篇: php 支付宝小程序授权登陆验签_星巴克