如果不为 0,则保留逗号后的值
Keep the values after comma if it is not 0
我有以下价值观
100,00
100,12
122,32
120,00
140,00
123,85
如果逗号后有 0,例如 100,00,那么我需要将其设置为 100。但是如果是 123,85,而逗号后没有 0,则我需要将其保留为 123,85
只需使用preg_replace
函数:
$result = preg_replace("/,00$/", "", $input);
如果要将123,10
更改为123,1
,请更改为/(,00|0)$/
。
您可以简单地匹配 ,
和 0
之后的所有内容。
/(\,0.*)/
片段:
<?php
$tests = [
'100,00',
'100,12',
'122,32',
'120,00',
'140,00',
'123,85'
];
foreach($tests as $test){
echo $test," => ",preg_replace("/(\,0.*)/","",$test),PHP_EOL;
}
我有以下价值观
100,00
100,12
122,32
120,00
140,00
123,85
如果逗号后有 0,例如 100,00,那么我需要将其设置为 100。但是如果是 123,85,而逗号后没有 0,则我需要将其保留为 123,85
只需使用preg_replace
函数:
$result = preg_replace("/,00$/", "", $input);
如果要将123,10
更改为123,1
,请更改为/(,00|0)$/
。
您可以简单地匹配 ,
和 0
之后的所有内容。
/(\,0.*)/
片段:
<?php
$tests = [
'100,00',
'100,12',
'122,32',
'120,00',
'140,00',
'123,85'
];
foreach($tests as $test){
echo $test," => ",preg_replace("/(\,0.*)/","",$test),PHP_EOL;
}