What regex in PHP would strip all characters from any input string except digits and the first encountered decimal point, and truncate everything to two digits after the first encountered decimal point (if more than one is present).
The input string should be interpreted from left to right, so string such as $34.455r.4r45,45.45
should yield the following output 34.45
.
Also, "any input string" means that strings with no dots, or no digits, and those having whatever other combination of characters are also possible as an input.
For example:
Input string Desired output
*$234.345 => 234.34
(9.9) => 9.9
$34.455r.4r45,45.45 => 34.45
2023-05-29 03:40:11Z, License: CC BY-SA 4.0 => 202305290340114.0
9.95,6.432,0.3 => 9.95
po2iaw5e.ro1i7im8jjks;fl32;i.u12ma => 25.17
oias25.spkks => 25.
4545 => 4545
oi.as2.5.6spkks => .25
oi.as..spkks => .
I tried this expression but it doesn't work
preg_replace('/^\d+(\.\d{0,2})?$/', '', $input_string);
I'm looking for a solution that is a single regular expression and replacement string to be used in one call to preg_replace()
.