x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 449

# need to extract price range from a text

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
rajccs
1 Solution

Commented:
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);
?>
``````
0

Commented:
Try dsmile code, but with:

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

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

IT GuruCommented:
And:

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

if you want to allow cents...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.