Skip to content

Instantly share code, notes, and snippets.

@robertuniqid
Created September 23, 2021 09:21
Show Gist options
  • Save robertuniqid/efbd00d44af1b73f00f51171487870f1 to your computer and use it in GitHub Desktop.
Save robertuniqid/efbd00d44af1b73f00f51171487870f1 to your computer and use it in GitHub Desktop.
Romania Validate CNP - PHP
<?php
// The goal is to prevent human error, not cheating the system.
class Sample {
private static $_cnpValidationConstant = "279146358279";
public static function isValidCNP( $cnp ) {
$cnp = intval( $cnp );
if( strlen( '' . $cnp ) !== ( strlen( self::$_cnpValidationConstant ) + 1 ) || !is_numeric( $cnp ) )
return false;
$security = $cnp % 10;
$checksum = 0;
$numberNavigation = floor( $cnp / 10 );
for( $currentDigit = $numberNavigation % 10, $currentPosition = strlen( self::$_cnpValidationConstant ) - 1;
$currentPosition >= 0;
$numberNavigation /= 10, $currentDigit = $numberNavigation % 10, $currentPosition-- )
$checksum += $currentDigit * self::$_cnpValidationConstant[ $currentPosition ];
$checksum = $checksum % 11;
return $checksum === 10 ? ( $security === 1 ) : $security === $checksum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment