Home > Backend Development > PHP Tutorial > php encoding conversion_PHP tutorial

php encoding conversion_PHP tutorial

WBOY
Release: 2016-07-13 16:58:49
Original
862 people have browsed it

Encoding conversion

This code is in the ThinkPHP framework. It seems to be very commonly used, so I picked it out separately and everyone can study it together.

function auto_charset($fContents,$from='',$to='')
{
if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
​​​ //If the encoding is the same or it is not a string scalar, it will not be converted
          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('Conversion failed');
                   return $fContents;
       }
}
elseif(is_array($fContents)){
foreach ( $fContents as $key => $val ) {
                                                                                                                                                                                                                                                     $_key                                                                 $fContents[$_key] = auto_charset($val,$from,$to);
If($key != $_key) {
                                                                                                                                                                                                                                                                                                                            }        }
          return $fContents;
}
elseif(is_object($fContents)) {
                        $vars = get_object_vars($fContents);
foreach($vars as $key=>$val) {
               $fContents->$key = auto_charset($val,$from,$to);
       }
          return $fContents;
}
else{
          return $fContents;
}
}


http://www.bkjia.com/PHPjc/631381.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631381.htmlTechArticleEncoding conversion This code is in the ThinkPHP framework. I feel it is very commonly used, so I will pull it out separately and let everyone learn together. . function auto_charset($fContents,$from='',$to='') { if( str...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template