I'm trying to extract a Price from a string:
Example:
$money='Rs.109.10';
$price=preg_replace('/[^0-9.]/u', '', $money);
echo $price;
Output of this example
.109.10
I'm expecting following output:
109.10
Help me to find correct regex.
preg_match('/(\d[\d.]*)/', $money, $matches);
$price = $matches[1];
or, better, as @Smamatti's answer suggests:
preg_match('/\d+\.?\d*/', $money, $matches);
$price = $matches[0];
ie. allows only one dot at max in the number. And no need for explicit capture since we want the whole match, here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With