Solved

Best way to strip dollar amounts of unwanted characters

Posted on 2016-08-01
2
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

729 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