Formatting numbers by tokens with php

Posted by Adam D on Stack Overflow See other posts from Stack Overflow or by Adam D
Published on 2010-03-16T04:07:19Z Indexed on 2010/03/16 7:36 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

I'm looking for a way to format numbers using "tokens". This needs to be conditional (for the first few leading characters).

Example:

<?php
 $styles=array('04## ### ###','0# #### ####','13# ###','1800 ### ###');
 format_number(0412345678); /*should return '0412 345 678'*/
 format_number(0812345678); /*should return '08 1234 5678'*/
 format_number(133622); /*should return '133 622'*/
 format_number(1800123456); /*should return '1800 123 456'*/
?>

Incase you haven't guessed, my use of this is to format Australian phone numbers, dependent on their 'type'.

I have a PHP function that does this, but it is ~114 lines and contains a lot of repeated code.

Can anyone help?

© Stack Overflow or respective owner

Related posts about php

Related posts about numbers