Home > Net >  PHP -How to extract prices with comma from text
PHP -How to extract prices with comma from text

Time:12-15

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?

CodePudding user response:

Try this

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

Result

4,99

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

  • Related