Solved

Best way to strip dollar amounts of unwanted characters

Posted on 2016-08-01
2
27 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 108

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now