Page 1 of 1

Regular Expressions help

Posted: Wed Mar 23, 2011 7:55 am
by ofir0803
Hi

i have this string:

Code: Select all

<div class="col-price">
    <h4>you save</h4>
 
    <span ><span class="currency">$</span>32</span>
    </div>
    <div class="col-price">
 
    <h4>discount</h4>
    <span >52%<span class="persent"></span></span>
    </div>
 
    <div class="col-price last">
    <h4>original price</h4>
    <span ><span class="currency">$</span>62</span>
 
    </div>
i want to be able to find the values of:
you save, discount and original price.
the desierd output: 32, 52, 62

how can i do it with Regular Expressions?

Re: Regular Expressions help

Posted: Wed Mar 23, 2011 12:22 pm
by akuji36
I believe you can do this without reg ex. If values are coming from mysql db
a column can be set so it formats numbers as normal currency (will show decimal places). Column type should be set to float and (6,2) under values.

take a look at the following:

http://www.experts-exchange.com/Databas ... 21171.html

It uses the select statement to select a value then divides result by 100.

Then take result and minus from original value to = you saved X dollars.

Re: Regular Expressions help

Posted: Wed Mar 23, 2011 12:38 pm
by Jonah Bron
@akuji36: it looks like he's scraping some HTML.

If that HTML there is character for character, this should work:

Code: Select all

$string = '<div class="col-price">
    <h4>you save</h4>
 
    <span ><span class="currency">$</span>32</span>
    </div>
    <div class="col-price">
 
    <h4>discount</h4>
    <span >52%<span class="persent"></span></span>
    </div>
 
    <div class="col-price last">
    <h4>original price</h4>
    <span ><span class="currency">$</span>62</span>
 
    </div>';

$format = '<div class="col-price">
    <h4>you save</h4>
 
    <span ><span class="currency">$</span>%d</span>
    </div>
    <div class="col-price">
 
    <h4>discount</h4>
    <span >%d%%<span class="persent"></span></span>
    </div>
 
    <div class="col-price last">
    <h4>original price</h4>
    <span ><span class="currency">$</span>%d</span>
 
    </div>';

sscanf($string, $format, $youSave, $discount, $originalPrice);
That puts the values into $youSave, $discount, and $originalPrice;

http://php.net/sscanf