Skip to content

Instantly share code, notes, and snippets.

@mauris
Created September 8, 2013 13:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mauris/6484795 to your computer and use it in GitHub Desktop.
Save mauris/6484795 to your computer and use it in GitHub Desktop.
A NRIC utility class implemented in PHP. For validating NRIC numbers or generating random NRIC numbers.
<?php
namespace Mauris\Utility;
class NricUtility
{
public static function validate($nric)
{
$nric = strtoupper($nric);
if (strlen($nric) == 9) {
$hash = self::checksum(substr($nric, 0, 8));
return $hash == $nric[8];
}
return false;
}
public static function checksum($nric)
{
$nric = strtoupper($nric);
if (strlen($nric) == 8) {
$prefix = $nric[0];
$nric = substr($nric, 1);
$number = $nric[0] * 2 + $nric[1] * 7 + $nric[2] * 6
+ $nric[3] * 5 + $nric[4] * 4 + $nric[5] * 3
+ $nric[6] * 2;
if ($prefix == 'T' || $prefix == 'G') {
$number += 4;
}
$mod = $number % 11;
$hash = array(
array('J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'),
array('X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K')
);
if (in_array($prefix, array('S', 'T'))) {
return $hash[0][$mod];
} elseif (in_array($prefix, array('F', 'G'))) {
return $hash[1][$mod];
}
}
}
public static function generate($limit = 1, $prefixes = array('S'))
{
$result = array();
for ($i = 0; $i < $limit; ++$i) {
$number = sprintf('%1$07d', mt_rand(0, 9999999));
$prefix = $prefixes[mt_rand(0, count($prefixes) - 1)];
$check = self::checksum($prefix . $number);
$result[] = $prefix . $number . $check;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment