Solved

php parse percentage or currency string to number

Posted on 2014-11-20
3
210 Views
Last Modified: 2014-11-20
Experts,

I've searched the manuals and tried their examples.  None seem to work.

Please provide a snippet to show how to store a post of 50% or $250.00 to  MySql as numbers.

Capt
0
Comment
Question by:Glen Gibb
3 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
ID: 40454763
Get rid of the '%' and the '$' because those are not part of numbers as far as MySQL (and most programming languages) are concerned.  With those characters, it can only be text, not numbers.  This bit of code may do what you need.

$punctuation = array("%", "$");
$onlynumbers = str_replace($punctuation, "", $numberstring);

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 450 total points
ID: 40454790
I use a regular expression to remove all of the non-numeric data, leaving only the +, -, ., and digits.  This is untested code, but I think I'm remembering it correctly...

$num = preg_replace('#[^0-9\+\-\.]#', NULL, $string);

Open in new window


Then I store the resulting values into MySQL columns defined as DECIMAL, using the appropriate bounds.  For dollars and cents you might choose DECIMAL(10,2).
0
 

Author Closing Comment

by:Glen Gibb
ID: 40455367
Thanks, Experts.  Easy peasy.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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 dynamically set the form action using jQuery.
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 …

708 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

18 Experts available now in Live!

Get 1:1 Help Now