3v4l.org

run code in 300+ PHP versions simultaneously
<?php $strings = [ "999TB2", "9999XX" ]; $re = '/^(?|(\d{3})([A-Z]{2})(\d)|(\d{2})(\d{2})([A-Z]{2}))$/'; foreach ($strings as $str) { echo preg_replace($re, '$1-$2-$3', $str) . PHP_EOL; }

preferences:
31.12 ms | 404 KiB | 5 Q