Skip to content

Instantly share code, notes, and snippets.

@Achterstraat
Created February 15, 2019 08:07
Show Gist options
  • Save Achterstraat/451efb0bad250d2fd4be1c0e819e08fb to your computer and use it in GitHub Desktop.
Save Achterstraat/451efb0bad250d2fd4be1c0e819e08fb to your computer and use it in GitHub Desktop.
Dutch license plates
function license($string, $validate = false)
{
$licenses = [
'specials' => [
'koninklijk' => '~^AA(\d{2})$~',
'buitenlands' => '~^(B|G)N\d{4}$~',
'diplomatiek' => '~^CD[ABFJNST](\d{1,3})$~',
'NAVO' => '~^RC(\d{4})$~',
'militair' => '~^(DF|(\d{2})?(K(A|L-Z)|L(M|O|U)|MC|DM))~',
'landbouw' => '~^GV~',
'speciaal' => '~^ZZ~',
],
'regulars' => [
'~^([A-z]{2})(\d{2})(\d{2})$~', // AB-12-34
'~^(\d{2})(\d{2})([A-z]{2})$~', // 12-34-AB
'~^(\d{2})([A-z]{2})(\d{2})$~', // 12-AB-34
'~^([A-z]{2})(\d{2})([A-z]{2})$~', // AB-12-CD
'~^([A-z]{2})([A-z]{2})(\d{2})$~', // AB-CD-12
'~^(\d{2})([A-z]{2})([A-z]{2})$~', // 12-AB-CD
'~^(\d{2})([A-z]{3})(\d{1})$~', // 12-ABC-3
'~^(\d{1})([A-z]{3})(\d{2})$~', // 1-ABC-23
'~^([A-z]{2})(\d{3})([A-z]{1})$~', // AB-123-C
'~^([A-z]{1})(\d{3})([A-z]{2})$~', // A-123-BC
'~^([A-z]{3})(\d{2})([A-z]{1})$~', // ABC-12-D
'~^([A-z]{1})(\d{2})([A-z]{3})$~', // A-12-BCD
'~^(\d{1})([A-z]{2})(\d{3})$~', // 1-AB-234
'~^(\d{3})([A-z]{2})(\d{1})$~', // 123-AB-4
'~^(\d{3})(\d{2})([A-z]{1})$~', // 123-45-A
'~^([A-z]{3})(\d{2})(\d{1})$~', // ABC-12-3
'~^([A-z]{3})([A-z]{2})(\d{1})$~', // ABC-DE-1
]
];
if(is_string($string))
{
$string = strtoupper(str_replace(['-', ' '], '', $string));
foreach($licenses['specials'] as $key => $special)
{
if(preg_match($special, $string))
{
return ($validate ? true : ucfirst($key));
}
}
if(strlen($string) === 6)
{
foreach($licenses['regulars'] as $key => $regular)
{
if(preg_match($regular, $string))
{
return ($validate ? true : preg_replace($regular, '$1-$2-$3', $string));
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment