php 转换数组的字符集,PHP 自动转换字符集(支持字符串和数组)功能实例
這篇文章主要為大家詳細介紹了PHP 自動轉換字符集(支持字符串和數組)功能實例,具有一定的參考價值,可以用來參考一下。
感興趣的小伙伴,下面一起跟隨512筆記的小編羅X來看看吧。
經測試代碼如下:
/**
* 自動轉換字符集(支持字符串和數組)
*
* @param
* @author 512筆記 www.512pic.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;
}
}
注:關于PHP 自動轉換字符集(支持字符串和數組)功能實例的內容就先介紹到這里,更多相關文章的可以留意512筆記的其他信息。
關鍵詞:字符集
您可能感興趣的文章
總結
以上是生活随笔為你收集整理的php 转换数组的字符集,PHP 自动转换字符集(支持字符串和数组)功能实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle php 配置,PHP +
- 下一篇: php fgetcsv 没有环绕符,PH