need to extract price range from a text

Posted on 2010-11-21
Last Modified: 2012-05-10
i need to extrac the price range like $XX-$XXX USD, from text. the X may be single digit or many digits to follow. any help with this.
Question by:rajccs
LVL 13

Accepted Solution

dsmile earned 500 total points
ID: 34182625
Try this


$pattern = '/(\$[0-9]+-\$[0-9]+ USD)/';

echo $string = <<< TXT

dummy text dummy text dummy text $112-$1324 USD dummy text dummy text 

dummy text dummy text $1312-$13624 USD dummy text dummy text dummy text $14412-$13244 USD dummy text dummy text 

dummy text $1012-$13244 USD dummy text dummy text dummy text $112-$13242 USD dummy text dummy text 


preg_match_all($pattern, $string, $result);



Open in new window

LVL 10

Expert Comment

ID: 34182723
Try dsmile code, but with:

$pattern = '/(\$[0-9]+)-(\$[0-9]+)/';

this will split every $XX-$XXX into lower and upper boundary
LVL 35

Expert Comment

by:Terry Woods
ID: 34193694

$pattern = '/(\$[0-9.]+)-(\$[0-9.]+)/';

if you want to allow cents...

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
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.

910 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