This is a WAY reboot, and a suggestion to return the leftmost digit is the best ...
function myfunc($md5) {
$total = 0;
foreach (str_split($md5) as $char) $total += ord($char);
return $total % 9 + 1;
}
echo myfunc("098f6bcd4621d373cade4e832627b4f6");
This way you can easily change the range of return values that interest you by changing the return status.
Or a more compact version:
function myfunc2($md5) {
return array_sum(array_map("ord", str_split($md5))) % 9 + 1;
}
You can even pass min and max as args:
function myfunc2($md5, $min = 1, $max = 9) {
return array_sum(array_map("ord", str_split($md5))) % $max + $min;
}
myfunc2("098f6bcd4621d373cade4e832627b4f6", 10, 20);
source
share