Solved

php parse percentage or currency string to number

Posted on 2014-11-20
3
223 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

22 Experts available now in Live!

Get 1:1 Help Now