Solved

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

Posted on 2011-09-08
2
409 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 500 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

792 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