Mutual conversion between php ascii codes and strings
[Code] php code:
<? Php
Class ascii {
/**
* Convert the ascii code into a string.
* @ Param type $ str the string to be decoded
* @ Param type $ prefix, default :&#
* @ Return type
*/
Function decode ($ str, $ prefix = "&#"){
$ Str = str_replace ($ prefix, "", $ str );
$ A = explode (";", $ str );
Foreach ($ a as $ dec ){
If ($ dec <128 ){
$ Utf. = chr ($ dec );
} Else if ($ dec <2048 ){
$ Utf. = chr (192 + ($ dec-($ dec % 64)/64 ));
$ Utf. = chr (128 + ($ dec % 64 ));
} Else {
$ Utf. = chr (224 + ($ dec-($ dec % 4096)/4096 ));
$ Utf. = chr (128 + ($ dec % 4096)-($ dec % 64)/64 ));
$ Utf. = chr (128 + ($ dec % 64 ));
}
}
Return $ utf;
}
/**
* Convert a string to an ascii code
* @ Param type $ c the string to be encoded
* @ Param type $ prefix, default :&#
* @ Return string
*/
Function encode ($ c, $ prefix = "&#"){
$ Len = strlen ($ c );
$ A = 0;
While ($ a <$ len ){
$ Ud = 0;
If (ord ($ c {$ a}) >=0 & ord ($ c {$ a}) <= 127 ){
$ Ud = ord ($ c {$ });
$ A + = 1;
} Else if (ord ($ c {$ a}) >=192 & ord ($ c {$ a}) <= 223 ){
$ Ud = (ord ($ c {$ a})-192) * 64 + (ord ($ c {$ a + 1})-128 );
$ A + = 2;
} Else if (ord ($ c {$ a}) >=224 & ord ($ c {$ a}) <= 239 ){
$ Ud = (ord ($ c {$ a})-224) * 4096 + (ord ($ c {$ a + 1})-128) * 64 + (ord ($ c {$ a + 2})-128 );
$ A + = 3;
} Else if (ord ($ c {$ a}) >=240 & ord ($ c {$ a}) <= 247 ){
$ Ud = (ord ($ c {$ a})-240) * 262144 + (ord ($ c {$ a + 1})-128) * 4096 + (ord ($ c {$ a + 2})-128) * 64 + (ord ($ c {$ a + 3})-128 );
$ A + = 4;
} Else if (ord ($ c {$ a}) >=248 & ord ($ c {$ a}) <= 251 ){
$ Ud = (ord ($ c {$ a})-248) * 16777216 + (ord ($ c {$ a + 1})-128) * 262144 + (ord ($ c {$ a + 2})-128) * 4096 + (ord ($ c {$ a + 3})-128) * 64 + (ord ($ c {$ a + 4})-128 );
$ A + = 5;
} Else if (ord ($ c {$ a}) >=252 & ord ($ c {$ a}) <= 253 ){
56 $ ud = (ord ($ c {$ a})-252) * 1073741824 + (ord ($ c {$ a + 1})-128) * 16777216 + (ord ($ c {$ a + 2})-128) * 262144 + (ord ($ c {$ a + 3})-128) * 4096 + (ord ($ c {$ a + 4})-128) * 64 + (ord ($ c {$ a + 5})-128 );
$ A + = 6;
} Else if (ord ($ c {$ a}) >=254 & ord ($ c {$ a}) <= 255) {// error
$ Ud = false;
}
$ Scill. = $ prefix. $ ud .";";
}
Return $ scill;
}
}
/*
Convert PHP to ASCII
Require_once "ascii_class.php ";
*/
$ Aa = new ascii;
Echo "<xmp> ";
Echo $ str = $ aa-> encode ("PHP secondary development: www. php2.cc ");
Echo "</xmp> ";
Echo $ aa-> decode ($ str );
?>