Mutual conversion between php ascii codes and strings

Source: Internet
Author: User

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 );

?>

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.