Question

All I want is to transform '1.234,56' into '1,234.56'...

I read about using array(s) as str_replace parameter, so I did this:

$value = '1.234,56';
$replacer1 = ',';
$replacer2 = '.';
echo \str_replace(array($replacer1, $replacer2), array($replacer2,$replacer1), $value);
//Prints '1,234,56' instead of '1,234.56'

The input and it's replacers are variable and it's not necessarily'll be used only for numbers.

Any ideas? Thanks...

Was it helpful?

Solution

Try this.i used number_format() for this

<?php

$number = "1.234,56";

$number = str_replace(array('.',','), array('','.'), $number);

echo number_format($number, 2, '.', ',');

You can also use money_format() function.Note that it won't work on windows.

Here is another method.

<?php

$str = "1.234,56";

$rp1 = '.';
$rp2 = ',';

//you might want to create a function for this.
if(false===strpos($str, '@')){

$str = str_replace($rp1, '@', $str);
$str = str_replace($rp2, $rp1, $str);
$str = str_replace('@', $rp2, $str);

}

echo $str;

You can also use a unique string like #!MYSEP!#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top