zoukankan      html  css  js  c++  java
  • PHP自动判断字符集并转码

    01    <?php
    02    function safeEncoding($string,$outEncoding ='UTF-8')   
    03    {   
    04        $encoding = "UTF-8";   
    05        for($i=0;$i<strlen($string);$i++)   
    06        {   
    07            if(ord($string{$i})<128)   
    08                continue;   
    09             
    10            if((ord($string{$i})&224)==224)   
    11            {   
    12                //第一个字节判断通过   
    13                $char = $string{++$i};   
    14                if((ord($char)&128)==128)   
    15                {   
    16                    //第二个字节判断通过   
    17                    $char = $string{++$i};   
    18                    if((ord($char)&128)==128)   
    19                    {   
    20                        $encoding = "UTF-8";   
    21                        break;   
    22                    }   
    23                }   
    24            }   
    25         
    26            if((ord($string{$i})&192)==192)   
    27            {   
    28                //第一个字节判断通过   
    29                $char = $string{++$i};   
    30                if((ord($char)&128)==128)   
    31                {   
    32                    // 第二个字节判断通过   
    33                    $encoding = "GB2312";   
    34                    break;   
    35                }   
    36            }   
    37        }   
    38                  
    39        if(strtoupper($encoding) == strtoupper($outEncoding))   
    40            return $string;   
    41        else  
    42            return iconv($encoding,$outEncoding,$string);   
    43    }
    44    ?>
  • 相关阅读:
    Gym 101194L / UVALive 7908
    POJ 2259
    POJ 2559
    Gym 101194E / UVALive 7901
    Gym 101194D / UVALive 7900
    一种整数集上二分的正确写法
    日常训练记录
    Gym 101194C / UVALive 7899
    Gym 101194A / UVALive 7897
    HDU 5542
  • 原文地址:https://www.cnblogs.com/afei-happy/p/3376376.html
Copyright © 2011-2022 走看看