Minor improvement in PhiLho's offer:
You can avoid substr by changing the regex to:
function FormatWithSlashes($number) { return preg_replace('/^0*(\d{3})(\d{3})(\d{3})$/', '$1/$2/$3', '0000' . $number); }
I also deleted ? after each of the first two capture groups, because when a 5, 6, or 7-digit number is given (as indicated in the question), it will always have at least 9 digits to work with. If you want to protect against the possibility of getting a smaller input number, run the regular expression with '000000000'. $ number .
Alternatively you can use
substr('0000' . $number, -9, 9);
and then merge the slash in appropriate places with substr_replace , which I suspect might be the fastest way to do this (no need to run regular expressions or perform splitting), but it really is just pointless optimization, since any of the solutions presented will be - still much faster than establishing a network connection to the server.
source share