Heim  >  Artikel  >  Backend-Entwicklung  >  php 字符串转hashcode(包括中文)

php 字符串转hashcode(包括中文)

WBOY
WBOYOriginal
2016-07-30 13:29:522035Durchsuche

//全角字符转变成半角字符

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( // 半角

        '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);  // 全角到半角

}

//字符转换为ascii码

function asc_encode($c)

{

    $len = strlen($c);

    $a = 0;

     while ($a

     {

        $ud = 0;

         if (ord($c{$a}) >=0 && ord($c{$a})

         {

            $ud = ord($c{$a});

            $a += 1;

         }

         else if (ord($c{$a}) >=192 && ord($c{$a})

         {

            $ud = (ord($c{$a})-192)*64 + (ord($c{$a+1})-128);

            $a += 2;

         }

         else if (ord($c{$a}) >=224 && ord($c{$a})

         {

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

         {

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

         {

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

         {

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

         { //error

            $ud = 0;

            $a++;

         }else{

             $ud = 0;

             $a++;

         }

         $scill .= "$ud";

     }

     return $scill;

}

//将字符串转变成hashcode

function hashCode($s){

    $arr_str = str_split($s);

    $len = count($arr_str);

    $hash = 0;

    for($i=0; $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下判断符号位

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

             //正数取前31位即可

             $hash &= 0x7fffffff;

        }

        else{

             //负数取前31位后要根据最小负数值转换下

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

        }

    }

    return $hash;

}

以上就介绍了php 字符串转hashcode(包括中文),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn