Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Regular expressions, end up with just dollar amount and decimal

Posted on 2011-09-08
2
Medium Priority
?
415 Views
Last Modified: 2012-05-12
I am trying to extract the dollar amount from a bunch of strings, that contain data other than just the dollar amount. So I would basically want to throw away everything that isn't a number or a period.
Some examples of input and what I want from the regex:
'$100.43 USD' > '100.43'
' $ 1,200 US' > '1200'
'' > '' //empty would still be empty.

Thanks!
0
Comment
Question by:interclubs
  • 2
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36505888
So something like this?
$result = preg_replace("/\D/", "", $source);

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36505894
Correction (I forgot about the decimal point!):
$result = preg_replace("/[^\d.]/", "", $source);

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

783 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