Skip to content

Instantly share code, notes, and snippets.

@lesstif
Last active May 20, 2022 09:56
Show Gist options
  • Save lesstif/75b15d515f3a83c1d079ee441d1e735f to your computer and use it in GitHub Desktop.
Save lesstif/75b15d515f3a83c1d079ee441d1e735f to your computer and use it in GitHub Desktop.
사업자 등록 번호 검증 php code
<?php
/**
* 사업자 번호 유효성 검증
*
* @param string $bizNumberParam 검증할 사업자 번호
*
* @return bool
*
* @see https://www.lesstif.com/pages/viewpage.action?pageId=93126973 사업자 번호 검증 규칙
*
*/
function validation_biz_number(string $bizNumberParam) : bool
{
// 검증 번호
$checkNumber = '137137135';
// 입력받은 사업자 등록 번호에서 숫자가 아닌 것은 제외
$bizNumber = preg_replace("/[^0-9]/", "", $bizNumberParam);
// 자릿수 체크
if (strlen($bizNumber) != 10)
return false;
// check digit
$validationKey = intval($bizNumber[9]);
$magicKey = 10;
$sum = 0;
// 각 자릿수를 서로 곱해서 더함
for ($i = 0; $i < 9; $i++) {
$bn = $bizNumber[$i];
$cn = $checkNumber[$i];
$sum += intval($bn) * intval($cn);
}
// 마지막에서 두 번째 숫자를 서로 곱한 후에 $magicKey 로 나눈 몫 구함.
$quotient = (intval($bizNumber[8]) * intval($checkNumber[8])) / $magicKey;
$sum += intval($quotient);
$remainder = $sum % $magicKey;
// 매직키인 10에서 나머지를 뺀 수가 사업자 등록 번호의 마지막 숫자와 일치해야 함.
if ( ($magicKey - $remainder) !== $validationKey) {
// $remainder 가 0 일 경우 마지막 값이 0 인지 확인
if ($remainder === 0 && $remainder === $validationKey) {
return true;
}
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment