🎉 initial commit
This commit is contained in:
28
classes/Application/Helper/NumberHash.php
Normal file
28
classes/Application/Helper/NumberHash.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user