Solved

Best way to strip dollar amounts of unwanted characters

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 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