- base_convert: documentation ( source)
- array_reverse: documentation ( source)
- strtoupper: documentation ( source)
- str_split: documentation ( source)
<?php
/**
* Get luhn checksum
*
* @param $str_string
* @param $int_base
* @return string
*/
function get_luhn_checksum($str_string, $int_base) {
$arr_chars = array_reverse(str_split($str_string));
$int_check_sum = 0;
foreach ($arr_chars AS $int_index=>$str_char) {
$int_adjustment = ((int)base_convert($str_char, $int_base, 10)) * ($int_index % 2 ? 1 : 2);
$int_check_sum += $int_adjustment;
echo "char $int_index is $str_char adjustment is +$int_adjustment running checksum is $int_check_sum\n";
}
echo "Final checksum is $int_check_sum modded = " . ($int_check_sum % $int_base) . "\n";
return strtoupper(base_convert($int_base - ($int_check_sum % $int_base), 10, $int_base));
}
echo get_luhn_checksum('7992739871', 10);
exit;
$str = get_luhn_checksum('0B022412500000009104', 16);
echo $str;