PHP 自动转换字符集(支持字符串和数组)功能实例
内容摘要
这篇文章主要为大家详细介绍了PHP 自动转换字符集(支持字符串和数组)功能实例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码
文章正文
这篇文章主要为大家详细介绍了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 | <code class = "php" > /** * 自动转换字符集(支持字符串和数组) * * @param * @author php教程 www.idcnote.com **/ function auto_charset( $fContents , $from , $to ){ $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 ; } } </code> |
注:关于PHP 自动转换字符集(支持字符串和数组)功能实例的内容就先介绍到这里,更多相关文章的可以留意
代码注释