Last active
July 9, 2022 17:41
-
-
Save kas-cor/6eb6c2961ab27b5299fc603da5c2911e to your computer and use it in GitHub Desktop.
Получить число из строки
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Получить число из строки | |
* @param string $string Исходная строка | |
* @param int $min Минимальная граница (0 по умолчанию) | |
* @param int $max Максимальная граница (100 по умолчанию) | |
* @param int $precision Точность от 1 до 32 (16 по умолчанию) | |
* @return int | |
*/ | |
function get_number_by_string(string $string, int $min = 0, int $max = 100, int $precision = 16): int | |
{ | |
if ($min >= $max) { | |
return 0; | |
} | |
// Максимальный хеш | |
$max_hash_number = hexdec(str_repeat('f', $precision)); | |
// Хеш по строке | |
$cur_hash_number = hexdec(substr(md5($string), 0, $precision)); | |
return round($cur_hash_number / $max_hash_number * ($max - $min)) + $min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment