Regular expression to get currency and amount from string

I have a regex with me preg_match('/(?<=\$)\d+(\.\d+)?\b/', $str, $regs that return me the amount in currency, but I'm trying to get a character, associated with this amount.

one). the string $300.00 asking price should return $ 300.00, but now it returns 300
2). the string EUR 300.00 should return EUR300.00, but now it returns 300

I just want a currency with an amount.

thanks

+4
source share
4 answers

First, you match the currency, which can be either $ or EUR , and then the extra empty space:

 (?:EUR|[$])\s* 

Then map the main group of numbers, followed by an optional period and two numbers:

 \d+(?:\.\d{2})? 

In general, we get the following:

 $pattern = '/(?:EUR|[$])\s*\d+(?:\.\d{2})?/'; if (preg_match($pattern, $string, $matches)) { echo $matches[0]; } 
+3
source

Try this one

 $str = "$300.00 asking price"; preg_match('/^([\$]|EUR|€)\s*([0-9,\s]*\.?[0-9]{0,2})?+/', $str, $regs); 

Outputs

 array (size=3) 0 => string '$300.00' (length=7) 1 => string '$' (length=1) 2 => string '300.00' (length=6) array (size=3) 0 => string 'EUR 300.00' (length=10) 1 => string 'EUR' (length=3) 2 => string '300.00' (length=6) 
+1
source

Try

 /(\$|EUR)\s*(\d*\.\d+)?\b/ 
0
source

If you want only part of the quantity for any character:

 preg_match_all("/([0-9]+[.]*)/", $t, $output_array); $output_array=implode("",$output_array[0]); 

"€ 12,451.5651 $ Euro" will be released: 12451.5651

0
source

Source: https://habr.com/ru/post/1498606/


All Articles