ASC Ⅱ code in php

Source: Internet
Author: User
About ASC Ⅱ code in php: this is a detailed page of ASC Ⅱ code in php. it introduces php, related knowledge, skills, experience, and some php source code.

Class = 'pingjiaf' frameborder = '0' src = 'http: // biancheng.dnbc?info/pingjia.php? Id = 336623 'Rolling = 'no'>

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.

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

Recently, a brother in the technical group raised the following question:

Format the auto-increment ID

A001-A999

B001-B999

......

Z001-Z999,

My initial idea was to split the if condition into A-Z letter in A loop,

However, this has a major disadvantage, and the code seems rigid and redundant. 26 English letters equals to 26 judgments.

Later, someone turned the letter into ASC Ⅱ code, which happens to be A-Z equal to 65-91 of ASC Ⅱ code;

In this way, you only need a function to format the ID:

Function format_string ($ num ){
$ Tag = floor ($ num-1)/999 );
// Part1 calculates the asc code
$ Part1 = 65 + $ tag;

// Part2 calculates the numeric part
$ Part2 = $ num-999 * $ tag;

$ A = strlen ($ part2 );

For ($ I = 0; $ I <(3-$ a); $ I ++)
{
$ B. = 0;
}
$ Str = chr ($ part1). $ B. $ part2;
Return $ str;
}

For ($ I = 1; I I <5000; $ I ++)
{
Echo $ str = format_string ($ I ).'
';
}

More articles on "ASC Ⅱ code in php"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/336623.html pageNo: 10.

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.