- function safeEncoding($string,$outEncoding ='UTF-8') {
- $encoding = "UTF-8";
- for($i=0;$i<strlen($string);$i++){
- if(ord($string{$i})<128)
- continue;
- if((ord($string{$i})&224)==224){
-
- $char = $string{++$i};
- if((ord($char)&128)==128){
-
- $char = $string{++$i};
- if((ord($char)&128)==128)
- {
- $encoding = "UTF-8";
- break;
- }
- }
- }
- if((ord($string{$i})&192)==192){
-
- $char = $string{++$i};
- if((ord($char)&128)==128){
-
- $encoding = "GB2312";
- break;
- }
- }
- }
-
- if(strtoupper($encoding) == strtoupper($outEncoding))
- return $string;
- else
- return iconv($encoding,$outEncoding,$string);
- }
- http://blog.csdn.net/yw5201314/article/details/8665099