Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Best way to strip dollar amounts of unwanted characters

Hi,
I have a simple form with one field... 'discount'
My users may type all kinds of crazy variations unfortunately. I'm hoping to get a dependable way of stripping it to the actual amount they mean.

Possible input:
$12,932.99
or
12,999
or
12999
etc.

I need to round up to the closet dollar
Remove zeros after decimal (including the decimal)
Get rid of any dollar signs etc. that aren't numbers

I've tried a few variations with rtrim, regex, num_format etc. but can't pull it all together.
Hoping someone can assist.
Below is just one attempt :)
$discount = rtrim($request->input('discount'), "0");
$discount = preg_replace('/[^\d]+/', '', $discount);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

Way better solution!