<?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; }
You have javascript disabled. You will not be able to edit any code.