Hola,

I am very new to regex and am relying on help from other forumers to help code this. If you think you can teach me (lol) even better.

I have an affiliate website that lists products from various external websites. For obvious reasons these products are sometimes out of stock and removed or the prices changed and my websites info is then defunct and innaccurate. I was given this very helpful code from a friend, using regex, that scans the external links page and then takes the price. I have rigged coding on my website to use this info to UPDATE mySQL accordingly - solving my problem.

Thing is, while it works for one on my supplier's website, it doesn't for others. I take it it has to do with the fact that the info within the parameter tags I'm declaring to search for is different from site to site.

Here's what I have, that works:

<?php

$url = 'http://www.cheapsmells.com/viewProduct.php?id=3978';

$parts = array ( array ( '<div class=\'productOriginalPrice\'>', '</div>' ), array ( '<div class=\'productOurPrice\'>', '</div>' ) );

$out = array ( 'original' => '', 'current' => '' );

if ( false !== ( $page = file_get_contents ( $url ) ) )
{
    foreach ( $parts AS $name => $value )
    {
        if ( false !== ( $s = stripos ( $page, $value[0] ) ) )
        {
            $page = substr ( $page, $s + strlen ( $value[0] ) );

            $out[($name == 0 ? 'original' : 'current' )] = preg_replace ( '/(.+?)(\d+\.\d+)(.+?)?/is', '\\2', substr ( $page, 0, strpos ( $page, $value[1] ) ) );
        }
    }
}

print  $out['current'];

?>

The info within parameter tags being "Our Price £xx.xx". If I need to change the parameter tags to <div class="value">&pound;34.00<font size="3"></font></div>, the info returned just being "&pound;34.00"

... how do I edit my coding. I know regex is extremely sensitive.

One last thing, while this code is set up to grab both original and current prices, only the current price will ever be needed.

Does anyone have a better code or a more effective way of me getting the info (on its own) from within the declared tags.

Many thanks and a happy new year

Hi halifaxer,

Great question... Using cURL, I came up with this:

<?php

$ch = curl_init('http://www.cheapsmells.com/viewProduct.php?id=3978');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$cr = curl_exec($ch);

preg_match_all('/([ a-z]+) (&pound;)([0-9]+\.[0-9]+)/i',$cr,$cp);

print_r($cp);

?>

When the script is run, you get this output:

Array
(
    [0] => Array
        (
            [0] => Original Price &pound;36.00
            [1] => Our Price &pound;22.54
            [2] => Saving &pound;13.46
        )

    [1] => Array
        (
            [0] => Original Price
            [1] => Our Price
            [2] => Saving
        )

    [2] => Array
        (
            [0] => &pound;
            [1] => &pound;
            [2] => &pound;
        )

    [3] => Array
        (
            [0] => 36.00
            [1] => 22.54
            [2] => 13.46
        )

)

Then you could loop through the array like this:

for($i=0; $i<count($cp[1]); $i++) echo $cp[1][$i] . ' is equal to ' . $cp[2][$i] . $cp[3][$i] . "\r\n";

The result would be:

Original Price is equal to &pound;36.00
Our Price is equal to &pound;22.54
Saving is equal to &pound;13.46

I'm not sure if that would be helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.