Home > Backend Development > PHP Tutorial > php custom UTF8 and cp1251 functions_PHP tutorial

php custom UTF8 and cp1251 functions_PHP tutorial

WBOY
Release: 2016-07-13 10:54:03
Original
1103 people have browsed it

php tutorial custom utf8 and cp1251 functions
When you need to convert some data from utf8, cp1251 (windows-1251) or cp1251 to utf8 you have to use the system function iconv. Common hosting providers do not allow this feature.
*/
function cp1251_to_utf8($s){
$c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
for($i=0; $i                  $c=ord($s[$i]);
If ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
                   elseif ($c>239) $t.=$c209.chr($c-112);
                  elseif ($c==184) $t.=$c209.$c209;
                    elseif ($c==168)                                                                                                           else $t.=$s[$i];
           }
             return $t;
}

        function utf8_to_cp1251($s)
        {
            for ($c=0;$c             {
               $i=ord($s[$c]);
               if ($i<=127) $out.=$s[$c];
                   if ($byte2){
                       $new_c2=($c1&3)*64+($i&63);
                       $new_c1=($c1>>2)&5;
                       $new_i=$new_c1*256+$new_c2;
                   if ($new_i==1025){
                       $out_i=168;
                   } else {
                       if ($new_i==1105){
                           $out_i=184;
                       } else {
                           $out_i=$new_i-848;
                       }
                   }
                   $out.=chr($out_i);
                   $byte2=false;
                   }
               if (($i>>5)==6) {
                   $c1=$i;
                   $byte2=true;
               }
            }
            return $out;
        }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632361.htmlTechArticlephp教程 自定义utf8和cp1251函数 当你需要从utf8转换一些数据,cp1251(窗- 1251)或cp1251到utf8你必须使用系统功能的iconv。常见的主机服务商不允...
Related labels:
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