: This article describes how to convert a php string to a hashcode (including Chinese characters). If you are interested in the PHP Tutorial, refer to it. // Convert full-width characters into half-width characters
Function replace_DBC2SBC ($ str ){
$ DBC = Array (
'0', '1', '2', '3', '4 ',
'5', '6', '7', '8', '9 ',
'A', 'B', 'C', 'D', 'e ',
'F', 'G', 'H', 'I', 'J ',
'K', 'L', 'M', 'n', 'O ',
'P', 'Q', 'R', 'S', 'T ',
'U', 'V', 'W', 'X', 'y ',
'Z', 'A', 'B', 'C', 'D ',
'E', 'F', 'G', 'H', 'I ',
'J', 'K', 'L', 'M', 'n ',
'O', 'P', 'Q', 'R', 'S ',
'T', 'u', 'V', 'W', 'X ',
'Y', 'z ','-','',':',
'.', '/', '% ','#',
'! ','@','&','(',')',
'<', '> ','"',''','? ',
'[', ']', '{', '}', '\',
'|', '+', '=', '_', '^ ',
'¥', 'Yun', 'hangzhou'
);
$ SBC = Array (// halfwidth
'0', '1', '2', '3', '4 ',
'5', '6', '7', '8', '9 ',
'A', 'B', 'C', 'D', 'e ',
'F', 'G', 'H', 'I', 'J ',
'K', 'L', 'M', 'n', 'O ',
'P', 'Q', 'R', 'S', 'T ',
'U', 'V', 'W', 'X', 'y ',
'Z', 'A', 'B', 'C', 'D ',
'E', 'F', 'G', 'H', 'I ',
'J', 'K', 'L', 'M', 'n ',
'O', 'P', 'Q', 'R', 'S ',
'T', 'u', 'V', 'W', 'X ',
'Y', 'z ','-','',':',
'.', '/', '% ','#',
'! ','@','&','(',')',
'<', '> ','"','\'','? ',
'[', ']', '{', '}', '\',
'|', '+', '=', '_', '^ ',
'$ ','~ ','''
);
Return str_replace ($ DBC, $ SBC, $ str); // full-width to half-width
}
// Convert characters to ascii codes
Function asc_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 = 0;
$ A ++;
} Else {
$ Ud = 0;
$ A ++;
}
$ Scill. = "$ ud ";
}
Return $ scill;
}
// Convert the string into hashcode
Function hashCode ($ s ){
$ Arr_str = str_split ($ s );
$ Len = count ($ arr_str );
$ Hash = 0;
For ($ I = 0; $ I <$ len; $ I ++ ){
If (ord ($ arr_str [$ I]) & gt; 127 ){
$ Ac_str = $ arr_str [$ I]. $ arr_str [$ I + 1]. $ arr_str [$ I + 2];
$ I + = 2;
} Else {
$ Ac_str = $ arr_str [$ I];
}
$ Hash = (int) ($ hash * 31 + asc_encode ($ ac_str ));
// 64 bit to determine the symbol bit
If ($ hash & 0x80000000) = 0 ){
// Take the first 31 digits as the positive number.
$ Hash & = 0x7fffffff;
}
Else {
// The negative number must be converted based on the minimum negative value after the first 31 digits.
$ Hash = ($ hash & 0x7fffffff)-2147483648;
}
}
Return $ hash;
}
The above introduces the conversion of php strings to hashcode (including Chinese characters), including some content, and hope to be helpful to friends who are interested in PHP tutorials.