Regular Expressions help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ofir0803
Forum Newbie
Posts: 22
Joined: Sun Jan 18, 2009 3:03 pm

Regular Expressions help

Post 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?
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Regular Expressions help

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Regular Expressions help

Post 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
Post Reply