Introduction: This is the detailed page of ASC ⅱ code in PHP. It introduces the related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 336623 'rolling = 'no'>
It took a lot of time to find a PHP code that can convert Chinese characters to ASCII codes.CodeUTF-8 is only one type of ASCII. Later found in the Manual
To batch conversion, and added a common ASCII code to restore characters. This code has been written for a while, and there is no time to post it. You can see that this class is not limited
It's just a conversion of Chinese.
<? PHP
Class
ASCII
{
Function
Decode
(
$ Str
)
{
Preg_match_all
(
"
/(D {2, 5 })/
"
,
$ Str
,
$
)
;
$
=
$
[
0
]
;
Foreach
(
$
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
)
;
$
=
0
;
While
(
$
<
$ Len
)
{
$ Ud
=
0
;
If
(
ORD
(
$ C
{
$
})
> =
0
&&
ORD
(
$ C
{
$
})
<=
127
)
{
$ Ud
=
ORD
(
$ C
{
$
})
;
$
+ =
1
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
192
&&
ORD
(
$ C
{
$
})
<=
223
)
{
$ Ud
=
(
ORD
(
$ C
{
$
})
-
192
)
*
64
+
(
ORD
(
$ C
{
$
+
1
})
-
128
)
;
$
+ =
2
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
224
&&
ORD
(
$ C
{
$
})
<=
239
)
{
$ Ud
=
(
ORD
(
$ C
{
$
})
-
224
)
*
4096
+
(
ORD
(
$ C
{
$
+
1
})
-
128
)
*
64
+
(
ORD
(
$ C
{
$
+
2
})
-
128
)
;
$
+ =
3
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
240
&&
ORD
(
$ C
{
$
})
<=
247
)
{
$ Ud
=
(
ORD
(
$ C
{
$
})
-
240
)
*
262144
+
(
ORD
(
$ C
{
$
+
1
})
-
128
)
*
4096
+
(
ORD
(
$ C
{
$
+
2
})
-
128
)
*
64
+
(
ORD
(
$ C
{
$
+
3
})
-
128
)
;
$
+ =
4
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
248
&&
ORD
(
$ C
{
$
})
<=
251
)
{
$ Ud
=
(
ORD
(
$ C
{
$
})
-
248
)
*
16777216
+
(
ORD
(
$ C
{
$
+
1
})
-
128
)
*
262144
+
(
ORD
(
$ C
{
$
+
2
})
-
128
)
*
4096
+
(
ORD
(
$ C
{
$
+
3
})
-
128
)
*
64
+
(
ORD
(
$ C
{
$
+
4
})
-
128
)
;
$
+ =
5
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
252
&&
ORD
(
$ C
{
$
})
<=
253
)
{
$ Ud
=
(
ORD
(
$ C
{
$
})
-
252
)
*
1073741824
+
(
ORD
(
$ C
{
$
+
1
})
-
128
)
*
16777216
+
(
ORD
(
$ C
{
$
+
2
})
-
128
)
*
262144
+
(
ORD
(
$ C
{
$
+
3
})
-
128
)
*
4096
+
(
ORD
(
$ C
{
$
+
4
})
-
128
)
*
64
+
(
ORD
(
$ C
{
$
+
5
})
-
128
)
;
$
+ =
6
;
}
Else
If
(
ORD
(
$ C
{
$
})
> =
254
&&
ORD
(
$ C
{
$
})
<=
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
);
//
Calculate the ASC code of Part1
$ Part1
=
65
+
$ Tag
;
//
Part2 calculates numbers
$ Part2
=
$ Num
-
999
*
$ Tag
;
$
=
Strlen
(
$ Part2
);
For
(
$ I
=
0
;
$ I
<
(
3
-
$
);
$ I
++
)
{
$ B
. =
0
;
}
$ Str
=
CHR
(
$ Part1
)
.
$ B
.
$ Part2
;
Return
$ Str
;
}
For
(
$ I
=
1
;
$ I
<
5000
;
$ I
++
)
{
Echo
$ Str
=
Format_string (
$ I
)
.
'
<Br/>
'
;
}
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.