RS7 RS7 - 1 year ago 77
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

Answer Source

You can do:

$str = preg_replace('/[^0-9,]|,[0-9]*$/','',$str); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download