Explanation of the php ascii code conversion class
Source: Internet
Author: User
In the past, it took a lot of time to find the php code that can convert Chinese characters to ascii codes. UTF-8 is only one type of ascii code. Later I found it in the manual, changed it to batch conversion, and added a common ascii code restoration character. This code has been written for a while, and there is no time to post it. you can see that this class is not just a conversion of Chinese. it took a lot of time before, find the php code that can convert Chinese characters to ascii codes. UTF-8 is also an ascii code. Later I found it in the manual, changed it to batch conversion, and added a common ascii code restoration character. This code has been written for a while, and there is no time to post it. you can see that this class is not just a conversion of Chinese.
<? Php
Class ascii
{
Function decode ($ str)
{
Preg_match_all ("/(d {2, 5})/", $ str, $ );
$ A = $ a [0];
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;
}
Function encode ($ c)
{
$ 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)
{
$ 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. = "& # $ ud ;";
}
Return $ scill;
}
}
?>
If you use any character set, the effect is the same. The effect address is as follows:
Http://qh8.net/test/class.ascii.php
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