PHP internal hashCode Function

后端 未结 5 710
孤独总比滥情好
孤独总比滥情好 2021-02-09 14:59

I am looking for the PHP equelent to JAVA\'s

 \"SomeString\".hashCode();

function. The hashCode i am looking for should be the same which is u

5条回答
  •  悲&欢浪女
    2021-02-09 15:30

    a utf-8 version with emoji support

    function str_hashcode($s){
        $hash = 0;
        $len = mb_strlen($s, 'UTF-8');
        if($len == 0 )
            return $hash;
        for ($i = 0; $i < $len; $i++) {
            $c = mb_substr($s, $i, 1, 'UTF-8');
            $cc = unpack('V', iconv('UTF-8', 'UCS-4LE', $c))[1];
            $hash = (($hash << 5) - $hash) + $cc;
            $hash &= $hash; // 16bit > 32bit
        }
        return $hash;
    }
    

提交回复
热议问题