Try this regex instead to include the decimal and commas:
\$(\d+(?:,\d+)*(?:\.\d+)?)
Click for Demo
Explanation:
\$
- matches $
(\d+(?:,\d+)*(?:\.\d+)?)
\d+
- matches 1 or more occurrences of a digit
(?:,\d+)*
- matches 0 or more occurences of substrings that start with a comma(,
) followed by 1+ digits
(?:\.\d+)?
- matches the decimal and fractional part of the number which has been made optional by placing a ?
at the end
Although the above regex will include ,
in the numbers matched, so when you try to perform calculations on such numbers, you will get an error. So, an alternative can be to remove the commas between the numbers and then perform those calculations as shown in the below code:
$pattern = '#\$(\d+(?:\.\d+)?)#';
$removeComma = '/(?<=\d),(?=\d)/m';
$string='En babyalarm kan koste alt fra $30 til $20.99, afhængigt af de funktioner, du ønsker. De fleste skærme kommer med et grundlæggende sæt funktioner, koster $3,000';
$string = preg_replace($removeComma, '', $string);
$string_with_price_replaced = preg_replace_callback($pattern, function($match) {
return (string)(number_format($match[1]*6.5, 0, "", ""));
}, $string);
echo $string_with_price_replaced;
Code output