RS7 RS7 - 6 months ago 22
PHP Question

Regex - Strip non numeric and remove cents if any

I'm currently working on a project in PHP and I'm in need of some Regex help. I'd like to be able to take a user inputted monetary value and strip all non numeric and decimal places/cents.


'2.000,00' to '2000'

'$ 2.000,00' to '2000'

'2abc000' to '2000'

'2.000' to 2000

(I'm using non US currency formatting)

How can I do this? I'd appreciate the help - Thanks


You can do:

$str = preg_replace('/[^0-9,]|,[0-9]*$/','',$str);