PHP Encoding Translator Code _php tutorial

Source: Internet
Author: User
Let's take a look at one of the Code conversion program codes written in PHP Oh, turn the gbk,utf-8 between each other.

function Phpunescape_no ($source) {
$DECODEDSTR = "";
$pos = 0;
$len = strlen ($source);
while ($pos < $len) {
$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 = "the". $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)! = "%") {//If it is an alphanumeric +-_. ASCII code
$c. = $val;
}
ElseIf (substr ($val,)! = "U") {//If the ASCII code is non-alphanumeric +-_.
$x = Hexdec (substr ($val,));
$c. =CHR ($x);
}
else {//If the code is greater than 0xFF
$val = Intval (substr ($val, 2), 16);
if ($val < 0x7F) {//0000-007f
$c. = Chr ($val);
}elseif ($val < 0x800) {//0080-0800
$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 < Mb_strlen ($string, $encoding); $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")
{
$ucs 2string = "";
$n =strlen ($STR);
for ($i =0; $i < $n; $i + +) {
$v = $str [$i];
$ord = Ord ($v);
if ($ord <=0x7f) {//0xxxxxxx
if ($order = = "little") {
$ucs 2string. = $v. chr (0);
}
else {
$ucs 2string. = Chr (0). $v;
}
}
ElseIf ($ord <0xe0 && ord ($str [$i +1]) >=0x80) {//110xxxxx 10xxxxxx
$a = (ord ($str [$i]) & 0x3F) <<6;
$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") {
$ucs 2string. = Chr ($l). chr ($h);
}
else {
$ucs 2string. = Chr ($h). chr ($l);
}
$i + +;
}elseif ($ord <0xf0 && ord ($str [$i +1]) >=0x80 && ord ($str [$i +2]) >=0x80) {//1110xxxx 10xxxxxx 10xxxxxx
$a = (ord ($str [$i]) & 0x1F) <<12;
$b = (ord ($str [$i +1]) & 0x3F) <<6;
$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") {
$ucs 2string. = Chr ($l). chr ($h);
}
else {
$ucs 2string. = Chr ($h). chr ($l);
}
$i +=2;
}
}
return $ucs 2string;
}


////////////////////////////////////////////////////////

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 (($MTxt, substr). substr ($MTxt,)), $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 (($MTxt, substr). substr ($MTxt,)), $TTXT);
$ETxt. = $ETxtstr [$k];
}
return $ETXT;
}


http://www.bkjia.com/PHPjc/445049.html www.bkjia.com true http://www.bkjia.com/PHPjc/445049.html techarticle Let's take a look at one of the Code conversion program codes written in PHP Oh, turn the gbk,utf-8 between each other. function Phpunescape_no ($source) {$decodedStr =; $pos = 0; $len = strlen ($source); W ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.