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

x
?
Solved

need to extract price range from a text

Posted on 2010-11-21
3
Medium Priority
?
447 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 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