Whats the cleanest way to convert a 5-7 digit number into xxx/xxx/xxx format in php?
php, regex
Solution
sprintf and modulo is one option
function formatMyNumber($num)
{
return sprintf('%03d/%03d/%03d',
$num / 1000000,
($num / 1000) % 1000,
$num % 1000);
}
Problem
I have sets of 5, 6 and 7 digit numbers. I need them to be displayed in the 000/000/000 format. So for example: 12345 would be displayed as 000/012/345 and 9876543 would be displayed as 009/876/543 I know how to do this in a messy way, involving a series of if/else statements, and strlen functions, but there has to be a cleaner way involving regex that Im not seeing.