简体   繁体   中英

Remove zero after and before Decimal Using php

Below code works fine but in second variable name fixed_2 why it shows 3 digit after decimal? I tried many ways didn't get any hope.

$text = "000892021.2408000";

$fixed_1 = preg_replace('/000/','',$text);
$fixed_2 = preg_replace('/.000/','',$text);
$fixed_3 = preg_replace('/000./','',$text);

var_dump($fixed_1);
var_dump($fixed_2);
var_dump($fixed_3);

output

 string(11) "892021.2408" string(13) "000892021.240" string(13) "92021.2408000" 

Any help please explain?

您可以使用嵌套的ltrim()rtrim()

    $res = ltrim(rtrim($text, '0'), '0');

Well the regular expression you used for $fixed_2 is ".000" . It means remove all strings that start with any character followed by 3 zeros. "." means any character, so it will match "8000" and will remove it. Thats why you got output of "000892021.240" in $fixed_2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM