PHP 自动转换编码代码,支持数组
内容摘要
这篇文章主要为大家详细介绍了PHP 自动转换编码代码,支持数组,具有一定的参考价值,可以用来参考一下。
对PHP自动转换编码代码,支持数组对此感兴趣的朋友,看看idc笔记做的技术笔
对PHP自动转换编码代码,支持数组对此感兴趣的朋友,看看idc笔记做的技术笔
文章正文
这篇文章主要为大家详细介绍了PHP 自动转换编码代码,支持数组,具有一定的参考价值,可以用来参考一下。
对PHP自动转换编码代码,支持数组对此感兴趣的朋友,看看idc笔记做的技术笔记!当我们在接受未知客户端提交的数据,由于各客户端的编码不统一,但在我们的服务器端最终只能以一种编码方式来处理,这种情况下就会涉及到编码转换问题1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <code class = "php" > /** * PHP自动转换编码代码,支持数组 * * @param * @arrange 五一二笔记网: 512pic.com **/ // 自动转换字符集 支持数组转换 function auto_charset( $fContents , $from = 'gbk' , $to = 'utf-8' ) { $from = strtoupper ( $from ) == 'UTF8' ? 'utf-8' : $from ; $to = strtoupper ( $to ) == 'UTF8' ? 'utf-8' : $to ; if ( strtoupper ( $from ) === strtoupper ( $to ) || empty ( $fContents ) || ( is_scalar ( $fContents ) && ! is_string ( $fContents ))) { //如果编码相同或者非字符串标量则不转换 return $fContents ; } if ( is_string ( $fContents )) { if (function_exists( 'mb_convert_encoding' )) { return mb_convert_encoding( $fContents , $to , $from ); } elseif (function_exists( 'iconv' )) { return iconv( $from , $to , $fContents ); } else { return $fContents ; } } elseif ( is_array ( $fContents )) { foreach ( $fContents as $key => $val ) { $_key = auto_charset( $key , $from , $to ); $fContents [ $_key ] = auto_charset( $val , $from , $to ); if ( $key != $_key ) unset( $fContents [ $key ]); } return $fContents ; } else { return $fContents ; } } /*** 来自php教程(www.idcnote.com) ***/ </code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <code class = "php" > /** * PHP自动转换编码代码,支持数组 * * @param * @arrange 五一二笔记网: 512pic.com **/ function isGb2312( $string ) { for ( $i =0; $i 127) { if ( ( $v >= 228) && ( $v < = 233) ) { if ( ( $i +2) >= ( strlen ( $string ) - 1)) return true; $v1 = ord( $string [ $i +1] ); $v2 = ord( $string [ $i +2] ); if ( ( $v1 >= 128) && ( $v1 < =191) && ( $v2 >=128) && ( $v2 < = 191) ) return false; else return true; } } } return true; } function isUtf8( $string ) { return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string ); } /*** 来自php教程(www.idcnote.com) ***/ </code> |
1 2 3 4 5 6 7 8 9 | <code class = "php" > /** * PHP自动转换编码代码,支持数组 * * @param * @arrange 五一二笔记网: 512pic.com **/ $str = isGb2312( $_GET [ 'str' ], 'gbk' ) ? iconv( 'gbk' , 'utf-8' , $_GET [ 'str' ]) : $_GET [ 'str' ]; /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于PHP 自动转换编码代码,支持数组的内容就先介绍到这里,更多相关文章的可以留意
代码注释