zoukankan      html  css  js  c++  java
  • PHP 判断字符的编码 并输出想要的编码格式字符 (转)

    1. /**
    2. * 判断字符编码  并输出想要的编码
    3. * Enter description here ...
    4. * @param unknown_type $string
    5. * @param unknown_type $outEncoding
    6. */ 
    7. function safeEncoding($string,$outEncoding ='UTF-8') { 
    8.         $encoding = "UTF-8"; 
    9.         for($i=0;$i<strlen($string);$i++){ 
    10.             if(ord($string{$i})<128) 
    11.             continue; 
    12.             if((ord($string{$i})&224)==224){ 
    13.                 //第一个字节判断通过 
    14.                 $char = $string{++$i}; 
    15.                 if((ord($char)&128)==128){ 
    16.                     //第二个字节判断通过 
    17.                     $char = $string{++$i}; 
    18.                     if((ord($char)&128)==128) 
    19.                     { 
    20.                         $encoding = "UTF-8"; 
    21.                         break; 
    22.                     } 
    23.                 } 
    24.             } 
    25.             if((ord($string{$i})&192)==192){ 
    26.                 //第一个字节判断通过 
    27.                 $char = $string{++$i}; 
    28.                 if((ord($char)&128)==128){ 
    29.                     // 第二个字节判断通过 
    30.                     $encoding = "GB2312"; 
    31.                     break; 
    32.                 } 
    33.             } 
    34.         } 
    35.           
    36.         if(strtoupper($encoding) == strtoupper($outEncoding)) 
    37.         return $string; 
    38.         else 
    39.         return iconv($encoding,$outEncoding,$string); 
    40.     } 
    41. http://blog.csdn.net/yw5201314/article/details/8665099
  • 相关阅读:
    EA教程 (四) SQLHelper类
    详解包含、扩展和泛化
    几种常用的单例模式详解
    我的分层
    EA教程(二)数据库
    软件版本号如何定义
    精解PV操作之信号量
    eclipse连接数据库驱动汇总
    [Leetcode 17] 13 Roman to Integer
    Short term goal for 2013 rest time
  • 原文地址:https://www.cnblogs.com/xihong2014/p/5337075.html
Copyright © 2011-2022 走看看