29 lines
649 B
PHP
29 lines
649 B
PHP
<?php
|
|
|
|
namespace Application\Helper;
|
|
|
|
class NumberHash
|
|
{
|
|
/**
|
|
* Return a number only hash
|
|
*
|
|
* @see https://stackoverflow.com/a/23679870/175071
|
|
*
|
|
* @param string $string string to hash
|
|
* @param null|int $length optional specific length
|
|
*
|
|
* @return int
|
|
*/
|
|
public static function numHash(string $string, ?int $length = null): int
|
|
{
|
|
$stringHash = md5($string, true);
|
|
$numberHash = unpack('N2', $stringHash);
|
|
$hash = $numberHash[1] . $numberHash[2];
|
|
if ($length) {
|
|
$hash = substr($hash, 0, $length);
|
|
}
|
|
|
|
return (int)$hash;
|
|
}
|
|
}
|