编码转换php
发布时间:2016-10-29 编辑:jiaochengji.com
教程集为您提供编码转换php等资源,欢迎您收藏本站,我们将为您提供最新的编码转换php资源
这段代码是ThinkPHP框架中的,感觉很常用,所以单独拎出来,大家共同学习一下。
function auto_charset($fContents,$from='''',$to='''')
{
if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
//如果编码相同或者非字符串标量则不转换
return $fContents;
}
$from = strtoupper($from)==''UTF8''? ''utf-8'':$from;
$to = strtoupper($to)==''UTF8''? ''utf-8'':$to;
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{
exit(''转换失败'');
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 ) {
&
function auto_charset($fContents,$from='''',$to='''')
{
if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
//如果编码相同或者非字符串标量则不转换
return $fContents;
}
$from = strtoupper($from)==''UTF8''? ''utf-8'':$from;
$to = strtoupper($to)==''UTF8''? ''utf-8'':$to;
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{
exit(''转换失败'');
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 ) {
&
您可能感兴趣的文章:
PHP 数组字符集编码转换的函数
php mb_convert_encoding文字编码转换函数
设置php页面编码的二种方法
学习php字符串编码的转换与判断
php 自动检测内容编码并转换的代码
php判断字符编码的二个方法
php获取字符串的编码格式的函数
php字符串编码转换函数 可以自动识别原编码
php字符串编码转换小例子
php时区转换函数示例
[关闭]