Solved

Best way to strip dollar amounts of unwanted characters

Posted on 2016-08-01
2
40 Views
Last Modified: 2016-08-01
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

0
Comment
Question by:tjyoung
2 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41737617
<?php // demo/temp_tjyoung.php
/**
 * https://www.experts-exchange.com/questions/28960760/Best-way-to-strip-dollar-amounts-of-unwanted-characters.html
 *
 * https://www.experts-exchange.com/articles/7830/A-Quick-Tour-of-Test-Driven-Development.html
 *
 */
error_reporting(E_ALL);
echo '<pre>';


function number_value($str, $ndec=2)
{
    $rgx
    = '#'         // REGEX DELIMITER
    . '['         // CHARACTER CLASS
    . '^'         // NEGATION - MATCH NONE OF THESE
    . '0-9-.'     // ACCEPTABLE CHARACTERS
    . ']'         // END CHARACTER CLASS
    . '#'         // REGEX DELIMITER
    ;
    $num = preg_replace($rgx, NULL, $str);
    $val = number_format($num, $ndec, '.', '');
    return $val;
}

$testdata = array
( '$12,932.99'
, '12,999'
, '12999'
, '-3'
)
;
foreach ($testdata as $value)
{
    echo PHP_EOL . $value;
    echo PHP_EOL . number_value($value, 0);
    echo PHP_EOL;
}

Open in new window

Outputs number values suitable for use in a MySQL query
$12,932.99
12933

12,999
12999

12999
12999

-3
-3

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41737824
Way better solution!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question