Jump to content

[SOLVED] Linking With External Website - Regex


e1seix

Recommended Posts

Complete regex novice here, but know can rely on your help.

 

I have an affiliate website stocking products through external website. As you can imagine the prices change a lot and it's hard to keep updating mySQL, so my idea is to use regex to automatically update.

 

I have a basic premise and I need some help:

 

The page I need to scan for info is - http://www.cheapsmells.com/viewProduct.php?id=3978

 

The tags I need to lift the info from within this page are - <div class='productOurPrice'>Our Price £22.54</div>

 

From here I need to just seperate the 22.54 part.

 

I can figure out the updating part etc, but I need help with the regex code/function/instruction initially. Can anyone help me get this far?

 

many thanks and happy new year  :P

Link to comment
Share on other sites

There is all kinds of ways to do it, you could just use a simple regex. Here some code I had lying around...

 

<?php

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

$item = '3978';

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

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

if ( false !== ( $page = file_get_contents ( $url . $item ) ) )
{
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_r ( $out );			


?>

Link to comment
Share on other sites

man, that is SOOO good! thank you, thank you. is there a simpler way of literally printing off the current price of

 

"22.54" instead of

"Array ( [current] => 22.54 [original] => 36.00 )"

 

as i need to grab this "22.54" as a defined variable and update the mysql data accordingly

Link to comment
Share on other sites

I have one last question...

 

This is great and works wonderfully especially with my php code

 

$qry = mysql_query("SELECT * FROM fragrances WHERE fragrances.avail='true' AND fragrances.sku=$sku UNION SELECT * FROM skincare WHERE skincare.avail='true' AND skincare.sku=$sku UNION SELECT * FROM shaving WHERE shaving.avail='true' AND shaving.sku=$sku UNION SELECT * FROM bodycare WHERE bodycare.avail='true' AND bodycare.sku=$sku UNION SELECT * FROM haircare WHERE haircare.avail='true' AND haircare.sku=$sku UNION SELECT * FROM suncare WHERE suncare.avail='true' AND suncare.sku=$sku")or
die(mysql_error());  

echo "<table>";

$i=0;
while ($row = mysql_fetch_array($qry))
{

$original=$row['pound'];

$url = $row[buy_link];

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

$out = array ( '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 = preg_replace ( '/(.+?)(\d+\.\d+)(.+?)?/is', '\\2', substr ( $page, 0, strpos ( $page, $value[1] ) ) );
	}
}
}

mysql_query("UPDATE fragrances SET pound='$out' WHERE sku='$sku'");
mysql_query("UPDATE skincare SET pound='$out' WHERE sku='$sku'");
mysql_query("UPDATE shaving SET pound='$out' WHERE sku='$sku'");
mysql_query("UPDATE bodycare SET pound='$out' WHERE sku='$sku'");
mysql_query("UPDATE haircare SET pound='$out' WHERE sku='$sku'");
mysql_query("UPDATE suncare SET pound='$out' WHERE sku='$sku'");

 

However the parameters in this case only relate to one of my affiliate sites and it defaults anything that doesn't match back to 0.00. I have tried to put an if statement around this but it doesn't make a difference.

 

Similarly I also had code:

 

mysql_query("UPDATE fragrances SET avail='NULL' WHERE sku='$sku'"); ...etc

which removes anything that doesn't fit within the parameters (ie. out of stock or not displayed) so that it removes it from my database, but I can't seem to place this or implement it properly either.

 

This is causing all sorts of problems for me. Has anyone any suggestions? Is this a common fault or is there just a problem with my structure and method?

 

I hope I'm explaining this correctly...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.