现在的位置: 首页 > 综合 > 正文

数组字符集转换

2018年04月14日 ⁄ 综合 ⁄ 共 762字 ⁄ 字号 评论关闭
$menus = $this->auto_charset($menus);//调用函数

function auto_charset($fContents, $from='utf-8', $to='gb2312') {
                $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 = $this->auto_charset($key, $from, $to);
                                $fContents[$_key] = $this->auto_charset($val, $from, $to);                                                                     
                                if ($key != $_key)                                                                                                             
                                        unset($fContents[$key]);                                                                                               
                        }                                                                                                                                      
                        return $fContents;                                                                                                                     
                }                                                                                                                                              
                else {                                                                                                                                         
                        return $fContents;                                                                                                                     
                }                                                                                                                                              
        }    

抱歉!评论已关闭.