Solved

Best way to strip dollar amounts of unwanted characters

Posted on 2016-08-01
2
48 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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 98
Change static entered data to dynamic with counter 5 32
php mysql pdo 13 30
Compiling PHP with Curl plus protocols 8 19
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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

679 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