チェックデジットを返す関数
2019年7月1日
・サンプルコード
function getCheckDigit($n){
$watch = 0;
$mod = 0;
$odd = 0;
$cd = 0;
$res = explode(',',chunk_split($n,1,','));
for($i = count($res)-1; $i >- 1; $i--)
{
$x = intval($res[$i]);
if($watch == 0)
{
$odd += $x;
$watch = 1;
}
else
{
$mod += $x;
$watch = 0;
}
}
$cd = (int)substr(strval(10 - intval(substr(strval($mod * 3 + $odd), -1))), -1);
return $cd;
}
var_dump(getCheckDigit(1000503345684000014));
var_dump(getCheckDigit(1000503409895000953));
・実行結果
