yhdleung/hkid-util

Utility functions for tasks related to Hong Kong Identity Card (HKID)

1.0.1 2022-04-07 17:16 UTC

This package is not auto-updated.

Last update: 2025-05-04 07:35:54 UTC


README

PHP Utility functions designed to help tasks related to Hong Kong Identity Card (HKID).

Latest Stable Version License

Features

  • Support two-letter prefix (e.g. AB987654(3))
  • For HKID input, parentheses are optional

Requirement

  • PHP 5.3+

Functions

  • getCheckDigit - Get the Check Digit of HKID
char HKIDUtil::getCheckDigit(string $id)
  • validateHKID - Validate HKID format
bool HKIDUtil::validateHKID(string $id)
  • randomHKID - Generate random HKID
string HKIDUtil::randomHKID(bool $hasParentheses=true)
  • validateDate - Validate date format in 'dd-mm-yyyy' or 'dd/mm/yyyy'
bool HKIDUtil::validateDate(string $date)

Installation

Include the utility function file

require_once("HKIDUtil.php");

Usage

Get the Check Digit of a HKID

$testID = 'C123456';
echo(HKIDUtil::getCheckDigit($testID));  // returns '9'

Validate HKID format, check digit parentheses are optional

var_dump(HKIDUtil::validateHKID('AB987654(3)'));   // bool(true)

if(HKIDUtil::validateHKID('AB9876543')){ 
    echo 'valid'; 
}     //returns 'valid'

Generate random HKID

echo(HKIDUtil::randomHKID());     // returns HKID, e.g. 'LA654668(9)'

for($i = 0; $i < 3; $i++){
    echo(HKIDUtil::randomHKID(0)) . PHP_EOL;
}
// returns HKID without parentheses, e.g. 
// 'Q2127047'
// 'J9009792'
// 'BA1196657'

Validate date format in 'dd-mm-yyyy' or 'dd/mm/yyyy'

var_dump(HKIDUtil::validateDate('31-12-1969'));     // bool(true)
var_dump(HKIDUtil::validateDate('01/01/1970'));     // bool(true)
var_dump(HKIDUtil::validateDate('1/1/1970'));       // bool(true)
var_dump(HKIDUtil::validateDate('30/02/1970'));     // bool(false)

License

See the LICENSE file for license rights and limitations (MIT).

Reference

This HKID validation formula is developed based on the information from Wikipedia.
維基百科, 香港身份證 (Wikipedia, Hong Kong Identity Card)

Contribution

Star, fork, pull request and issue report are all welcome.