Solved

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

Posted on 2011-09-08
2
411 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
[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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON decode 5 46
Insert PHP into HTML page. 7 52
foreach loop to only capture results that are greater than 0 16 42
PHP, Curl and XML 13 17
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
The viewer will learn how to count occurrences of each item in an array.

752 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