PHP internal hashCode Function

后端 未结 5 713
孤独总比滥情好
孤独总比滥情好 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:05

    Here is my 2 cents for implementing Java's hashCode in PHP:

    /**
     * Simulates java hashCode function
     * hash a string to 32 bit
     * @param str the string to hash
     * @return hashed 32 bit integer
     */
    function hashCode($str) {
        $str = (string)$str;
        $hash = 0;
        $len = strlen($str);
        if ($len == 0 )
            return $hash;
    
        for ($i = 0; $i < $len; $i++) {
            $h = $hash << 5;
            $h -= $hash;
            $h += ord($str[$i]);
            $hash = $h;
            $hash &= 0xFFFFFFFF;
        }
        return $hash;
    };
    
    0 讨论(0)
  • 2021-02-09 15:10

    Arkh and the github solution referenced by guiguoz are in the right direction, but both fail to take into account that PHP will upconvert the integer hash value to a double as soon as it exceeds 2^61. The java function, which is calculated using fixed hardware 32-bit signed values, involves 32-bit arithmetic overflow (intrinsic to the CPU) to keep the value as a 32-bit signed integer.

    In PHP, you will need to manually perform that arithmetic overflow each time the $hash is updated:

    function overflow32($v)
    {
        $v = $v % 4294967296;
        if ($v > 2147483647) return $v - 4294967296;
        elseif ($v < -2147483648) return $v + 4294967296;
        else return $v;
    }
    
    function hashCode( $s )
    {
        $h = 0;
        $len = strlen($s);
        for($i = 0; $i < $len; $i++)
        {
            $h = overflow32(31 * $h + ord($s[$i]));
        }
    
        return $h;
    }
    

    (edit: corrected %v typo)

    0 讨论(0)
  • 2021-02-09 15:13

    There is no such method available in php. So you will have to implement the correct method. Wikipedia gives the algorithm used by Java.lang.hashCode which is used by strings I think, so here is a quick php version of it:

    <?php
    function getStringHashCode($string){
      $hash = 0;
      $stringLength = strlen($string);
      for($i = 0; $i < $stringLength; $i++){
        $hash = 31 * $hash + $string[$i];
      }
      return $hash;
    }
    
    0 讨论(0)
  • 2021-02-09 15:28

    spl_object_hash is probably the closest to what you want, but despite the name it does not really return a hash of the passed in value, merely an internal unique identifier. I don't know if it's the hash actually used under the hood for arrays etc.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题