I would like to ask for help with this, I wanted to check for a string
matching a decimal followed by a letter (15M, 15.3M, 15T or 15.3T) and
replace it with zeroes.
Like these:
15M -> 15000000
15.3M -> 15300000
15T -> 15000
15.3T -> 15300
I tried doing this with str_replace but can’t get it right. Hope
someone can help me.
“T” could be “thousand” or “trillion”, you know.
prints:
edit:
Anchoring (makes any garbage on the front or back mean no match):
You may want to make whitespace allowed, however:
You can also use “\d” in place of “[0-9]:
Case insensitivity:
Optional factor:
Use printf to control output formatting:
Stephan202 recommended a clever trick, put the “?” (optional) within the parens and you’re guaranteed a match string, it just might be blank. Then you can use the blank as an array key to get the default value without having to test whether it matched or not.
Putting all that together: