Solved

need to extract price range from a text

Posted on 2010-11-21
3
439 Views
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.
0
Comment
Question by:rajccs
3 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 34182625
Try this
<pre>
<?php
$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 
TXT;
preg_match_all($pattern, $string, $result);
print_r($result);
?>

Open in new window

0
 
LVL 10

Expert Comment

by:jfromanski
ID: 34182723
Try dsmile code, but with:

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

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

Expert Comment

by:Terry Woods
ID: 34193694
And:

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

if you want to allow cents...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

830 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