How can I add a period after the first three characters and then one 'every 3 characters after?

advertisements

How can I add a period after the first three characters and then a ' every 3 characters after?

Example
$number = 1100000
Output = 1'100.000

Example2
$number = 560000
Output = 560.000

Example3
$number = 1000256000
Output = 1'000'256.000

I tried number_format but it will add the same symbol (. , ')...thanks!!

$number = 1100000;
$english_format_number = number_format($number, 0, '.', "'");
Output: 1'100'000

$number = 1100000;
$english_format_number = number_format($number, 3, '.', "'");
Output: 1'100'000.000

$number = 1100000;
echo chunk_split($number,1,".");

Thanks!!


You were on the right track with number_format but you have to specify all 4 parameters to achieve the result you are looking for. You also need to divide the number by 1000 to avoid the extra decimal places.

echo number_format($number / 1000, 3, "." , "'");

See: http://php.net/manual/en/function.number-format.php

PHPFiddle: http://phpfiddle.org/main/code/e2c-a6q