djekl
6/26/2014 - 8:37 AM

Simple function to check for a valid NHS number (with example usage)

Simple function to check for a valid NHS number (with example usage)

<?php

// start with our test NHS number
$nhs_no = '401 023 2137';

function validNhsNumber($nhs_no)
{
    // save the input number for the exception
    $input_nhs_no = $nhs_no;

    // stip all non alpha numeric
    $nhs_no = preg_replace("/[^0-9]/ui", "", $input_nhs_no);

    // check the length
    if (strlen($nhs_no) <> 10) {
        throw new Exception("Bad NHS number ({$input_nhs_no})");
    }

    // explode the string into an array
    $nhs_no = str_split($nhs_no);

    // get our checksum val from the input
    $nhs_checksum = (int)array_pop($nhs_no);

    // set an empty checksum
    $checksum = 0;

    // now we have to do the math...
    foreach ($nhs_no as $key => $value) {
        $checksum += (int)$value * (10 - (int)$key);
    }

    // divide the checksum by 11
    $checksum = $checksum % 11;

    // remove 11 from the checksum
    $checksum = 11 - $checksum;

    // if the checksum is 10 then its invalid
    if ($checksum === 10) {
        throw new Exception("Bad NHS number ({$input_nhs_no})");
    }

    // if the checksum is 11, then set it to 0
    if ($checksum === 11) {
        $checksum = 0;
    }

    // if the checksum matches the nhs_checksum then were valid
    if ($checksum <> $nhs_checksum) {
        throw new Exception("Bad NHS number ({$input_nhs_no})");
    }

    // we have a valid NHS number (mathmatically)
    return preg_replace("/[^0-9]/ui", "", $input_nhs_no);
}

try {
    $valid_nhs_no = validNhsNumber($nhs_no);
} catch (Exception $e) {
    print $e->getMessage() . PHP_EOL;
}

if (!empty($valid_nhs_no)) {
    print "YAY! {$valid_nhs_no} is a valid NHS Number\r\n";
}