문제

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...

도움이 되었습니까?

해결책

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!#

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top