PHP string hashcode (including Chinese)

Source: Internet
Author: User
Full-width characters converted to 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 ', '-', ', ': ',

'. ' , ',' , '/' , '%' , '#' ,

'! ' , '@' , '&' , '(' , ')' ,

' ' ', ' ', ' ' ', ', ', '? ' ,

'[' , ']' , '{' , '}' , '\' ,

' | ', ' + ', ' = ', ' _ ', ' ^ ',

' ¥ ', '  ̄ ', ' '

);

$SBC = Array (//Half Corner

' 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 angle to half angle

}

Convert character to ASCII code

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

$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 a 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]) >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));

64bit judging the symbol bit

if (($hash & 0x80000000) = = 0) {

Positive number takes the first 31 digits

$hash &= 0x7fffffff;

}

else{

Negative numbers take the first 31 digits to be converted according to the minimum negative value

$hash = ($hash & 0x7fffffff)-2147483648;

}

}

return $hash;

}

The above describes the PHP string to hashcode (including Chinese), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.