Last active
January 25, 2023 13:01
-
-
Save matriphe/3103ec578ec556bad5047b378520f070 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$examples = [ | |
// kendaraan umum | |
'B 1', | |
'B 1234 ABC', | |
'B-1234-ABC', | |
'AD-1234 - XYZ', | |
'AB1234LD', | |
'D1', | |
// kendaraan diplomat | |
'CD 49 11', // nomor kendaraan korps diplomatik Jepang | |
'CD 100 10', // nomor kendaraan korps diplomatik ASEAN | |
'B 81879 25', // nomor kendaraan operasional staf kedutaan Filipina | |
'B 12345 100', // nomor kendaraan operasional staf kedutaan ASEAN | |
// kendaraan militer dan kepolisian | |
'1993-07', // nomor kendaraan kepolisian | |
'1 - 00', | |
'00 - VI', | |
'12345-I', | |
// format salah | |
'N 0 0', | |
'N 0 N', | |
'A 0' | |
]; | |
foreach ($examples as $e) { | |
$formatted = format($e); | |
if (!is_null($formatted)) { | |
printf("%s adalah nomor kendaraan Indonesia, dengan format %s\n", $e, $formatted); | |
} else { | |
printf("%s bukan nomor kendaraan Indonesia\n", $e); | |
} | |
} | |
// To format | |
function format($string) | |
{ | |
$string = strtoupper(trim($string)); | |
$pattern = '/^([A-Z]{1,3})(\s|-)*([1-9][0-9]{0,3})(\s|-)*([A-Z]{0,3}|[1-9][0-9]{1,2})$/i'; | |
if (preg_match($pattern, $string)) { | |
return trim(strtoupper(preg_replace($pattern, '$1 $3 $5', $string))); | |
} | |
// militer dan kepolisian | |
$pattern = '/^([0-9]{1,5})(\s|-)*([0-9]{2}|[IVX]{1,5})*/'; | |
if (preg_match($pattern, $string)) { | |
return trim(strtoupper(preg_replace($pattern, '$1-$3', $string))); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ibnux 👏 terima kasih juga infonya untuk plat nomor khusus militer dan kepolisian. skrip sudah saya ubah dan kini bisa mengenali dan memformat plat nomor kepolisian dan militer.