这将完成工作,但您当然也可以使用 preg_replace 提出不同的方法。
<?php
$myValue = '1.099,90';
$parts = explode(".", $myValue); // break up the (.)periods
$num = count($parts); // number of parts
for($loop = 0; $loop < $num; $loop++){ // cycle through each part
if(strpos($parts[$loop], ",") !== false){ // if this includes (,)comma - swap it
$parts[$loop] = str_replace(",", ".", $parts[$loop]);
}
if($loop !== ($num - 1)){ // if this is not the last loop iteration..add comma after (replace period)
$myNewValue .= $parts[$loop] . ",";
} else {
$myNewValue .= $parts[$loop]; // last loop iteration, no comma at end
}
}
echo $myNewValue;
您还可以使用 str_replace w/ extra | 符号(或任何东西)...
<?php
$myValue = '1.099,90';
$replace = array(",", ".", "|");
$with = array("|", ",", ".");
$myNewValue = str_replace($replace, $with, $myValue);
echo $myNewValue;
?>