Amina
Amina

Reputation: 682

PHP -How to extract prices with comma from text

I'm trying to extract numbers from a text while keeping the comma For example I have this text "87,45 €" so I would like to extract only 87,4 and change it to a number to perform calculations Example 2 "+ 4,99 € Tax" I would like to extract 4,99 .

I found this solution but it is removing the comma , it will output 8745

$int = (int) filter_var($productPrice, FILTER_SANITIZE_NUMBER_INT);

Is there a way to convert to a number and keep the comma , because those are prices?

Upvotes: 1

Views: 265

Answers (1)

Oliver Kucharzewski
Oliver Kucharzewski

Reputation: 2655

Try this

echo preg_replace('([^\d,.]+)', '', '4,99 € Tax');

Result

4,99

This will replace anything that isnt a digit \d or period .

Upvotes: 2

Related Questions