您好,欢迎访问一九零五行业门户网

php 编码转换程序代码_PHP教程

我们来看看用php写的一款编码转换程序代码哦,把gbk,utf-8之间互转等。
function phpunescape_no($source) {
    $decodedstr = ;
    $pos = 0;
    $len = strlen ($source);
    while ($pos         $charat = substr ($source, $pos, 1);
        if ($charat == '%') {
            $pos++;
            $charat = substr ($source, $pos, 1);
            if ($charat == 'u') {
                // we got a unicode character
                $pos++;
                $unicodehexval = substr ($source, $pos, 4);
                $unicode = hexdec ($unicodehexval);
                $entity = . $unicode . ';';
                $decodedstr .= utf8_encode ($entity);
                $pos += 4;
            }
            else {
                // we have an escaped ascii character
                $hexval = substr ($source, $pos, 2);
                $decodedstr .= chr (hexdec ($hexval));
                $pos += 2;
            }
        } else {
            $decodedstr .= $charat;
            $pos++;
        }
    }
    return $decodedstr;
}
//////////////////////////////////////////////////////////////////////
function phpunescape($escstr){
  preg_match_all(/%u[0-9a-za-z]{4}|%.{2}|*|[0-9a-za-z.+-_]+/,$escstr,$matches); //prt($matches);
  $ar = &$matches[0];
  $c = ;
  foreach($ar as $val){
  if (substr($val,0,1)!=%) { //如果是字母数字+-_.的ascii码
      $c .=$val;
  }
  elseif (substr($val,1,1)!=u) { //如果是非字母数字+-_.的ascii码
    $x = hexdec(substr($val,1,2));
      $c .=chr($x);
  }
  else { //如果是大于0xff的码
    $val = intval(substr($val,2),16);
    if($val       $c .= chr($val);
    }elseif($val       $c .= chr(0xc0 | ($val / 64));
      $c .= chr(0x80 | ($val % 64));
    }else{                // 0800-ffff
       $c .= chr(0xe0 | (($val / 64) / 64));
       $c .= chr(0x80 | (($val / 64) % 64));
       $c .= chr(0x80 | ($val % 64));
     }
  }
  }
  return utftogbk($c);
}
function utftogbk($cs){
 $codeobj = new chinese(utf8,gbk);
 return $codeobj ->convert($cs);
}
function phpescape($str){
 $returnstr = ;
 if (@function_exists('mb_convert_encoding')){
  $returnstr = phpescape_yes($str);
 }
 else{
  $returnstr = phpescape_no($str);
 }
 return $returnstr;
}
function phpescape_yes($string, $encoding = 'gbk') {
  $return = '';
  for ($x = 0; $x     $str = mb_substr($string, $x, 1, $encoding);
    if (strlen($str) > 1) {
      $return .= '%u' . strtoupper(bin2hex(mb_convert_encoding($str, 'ucs-2', $encoding)));
    } else {
      $return .= '%' . strtoupper(bin2hex($str));
    }
  }
  return $return;
}
function phpescape_no($str){
    preg_match_all(/[x80-xff].|[x01-x7f]+/,$str,$newstr);
    $ar = $newstr[0];
    foreach($ar as $k=>$v){
        if(ord($ar[$k])>=127){
   $tmpstring=bin2hex(gbktoucs2($v));
            if (!eregi(win,php_os)){
                $tmpstring = substr($tmpstring,2,2).substr($tmpstring,0,2);
            }
            $restring.=%u.$tmpstring;
        } else {
            $restring.= rawurlencode($v);
        }
    }
    return $restring;
}
function gbktoucs2($cs){
 $codeobj = new chinese(gbk,utf8);
 return utf8tounicode($codeobj ->convert($cs));
}
function utf8tounicode($str,$order=big)
{
  $ucs2string =;
    $n=strlen($str);
    for ($i=0;$i  $v = $str[$i];
  $ord = ord($v);
  if( $ord     if ($order==little) {
       $ucs2string .= $v.chr(0);
   }
   else {
       $ucs2string .= chr(0).$v;
   }
  }
  elseif ($ord=0x80) {  //110xxxxx 10xxxxxx
   $a = (ord($str[$i]) & 0x3f )   $b =  ord($str[$i+1]) & 0x3f ;
   $ucscode = dechex($a+$b);   //echot($ucscode);
   $h = intval(substr($ucscode,0,2),16);
   $l  =  intval(substr($ucscode,2,2),16);
   if ($order==little) {
       $ucs2string   .= chr($l).chr($h);
   }
   else {
        $ucs2string   .= chr($h).chr($l);
   }
   $i++;
  }elseif ($ord=0x80  && ord($str[$i+2])>=0x80) { //1110xxxx 10xxxxxx 10xxxxxx
      $a = (ord($str[$i]) & 0x1f)   $b = (ord($str[$i+1]) & 0x3f )   $c =  ord($str[$i+2]) & 0x3f ;
   $ucscode = dechex($a+$b+$c);   //echot($ucscode);
   $h = intval(substr($ucscode,0,2),16);
   $l  =  intval(substr($ucscode,2,2),16);
   if ($order==little) {
       $ucs2string   .= chr($l).chr($h);
   }
   else {
        $ucs2string   .= chr($h).chr($l);
   }  
   $i +=2;
  }
    }
 return $ucs2string;  
}
////////////////////////////////////////////////////////
function unescapefuncmake($txt){
 if ($txt[2]!=*) return $txt;
 $etxt = ;
    $mtxt = egy+nb@qwxvcwjkprxvzdl/h7eomtsa9f6*fpnr81i_0kqdg2lbcuziajyo34m-st%5.uh3syz0hzt/y@qdtnecf1bpujio.x6ks+oir8gpvg9wbm%xjvklwrn*f4hae-qladm27uc5_;
 $ttxtnum = substr($txt,0,2);
 $ttxt = substr((substr($mtxt,70).substr($mtxt,70)),$ttxtnum);
 for ($ii=3; $ii  $w = ($ii-3)%10;
  $k = strpos($ttxt,$txt[$ii],$w)-$w;
  $etxt .= $mtxt[$k];
 }
 return phpunescape($etxt);
}
function escapefuncmake($txt){
 if ($txt== || $txt[2]==*) return $txt;
 $mtxt = egy+nb@qwxvcwjkprxvzdl/h7eomtsa9f6*fpnr81i_0kqdg2lbcuziajyo34m-st%5.uh3syz0hzt/y@qdtnecf1bpujio.x6ks+oir8gpvg9wbm%xjvklwrn*f4hae-qladm27uc5_;
 $btxt = phpescape($txt);
 $ttxt = floor(mt_rand(0,50));
 $etxt = $ttxt.($ttxt>9?*:**);
 for ($ii=0; $ii  $k = strpos($mtxt,$btxt[$ii])+($ii%10);
  $etxtstr = substr((substr($mtxt,70).substr($mtxt,70)),$ttxt);
  $etxt .= $etxtstr[$k];
 }
 return $etxt;
}
http://www.bkjia.com/phpjc/445049.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/445049.htmltecharticle我们来看看用php写的一款编码转换程序代码哦,把gbk,utf-8之间互转等。 function phpunescape_no($source) { $decodedstr = ; $pos = 0; $len = strlen ($source); w...
其它类似信息

推荐信息