shaunie Posted February 15, 2011 Share Posted February 15, 2011 Hi, I have the written the following code which scrapes price info from a website: $url = 'http://www.mydomain.com'; $html = file_get_contents($url); $pattern = '/<span class="price">(.*?)<\/span>/'; preg_match_all($pattern, $html, $matches); print_r($matches); It works well however I need to add in the delivery cost to each array element with a different pattern: /<span class="delivery">(.*?)<\/span>/'; Any idea how i can do this so each array element has both the price and delivery costs in a two dimensional array? Thanks for your advice Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 15, 2011 Share Posted February 15, 2011 Not tested: $url = 'http://www.mydomain.com'; $html = file_get_contents($url); preg_match_all('/<span class="price">(.*?)<\/span>/', $html, $prices); preg_match_all('/<span class="delivery">(.*?)<\/span>/', $html, $deliveries); foreach($prices[1] as $key => $price) { $result[$key]['price'] = $price; if(isset($deliveries[1][$key])) { $result[$key]['delivery'] = $deliveries[1][$key]; } } Or if you're sure the arrays are the same length: foreach($prices[1] as $key => $price) { $result[] = array('price' => $price, 'delivery' => $deliveries[1][$key]); } Quote Link to comment Share on other sites More sharing options...
shaunie Posted February 16, 2011 Author Share Posted February 16, 2011 Hi, Thanks for your reply, it works great, the problem I have now found is that some of the prices on this site have free delivery and in this case the html is different: <span class="free_delivery"> Is there a way I can match up the elements in the array correctly in this scenario? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 16, 2011 Share Posted February 16, 2011 Is there anything in that span like $0 or something, or just an empty span? Well, you might try this pattern: preg_match_all('/<span class="(?:free_)?delivery">(.*?)<\/span>/', $html, $deliveries); Quote Link to comment Share on other sites More sharing options...
shaunie Posted February 16, 2011 Author Share Posted February 16, 2011 Thanks for your reply, I'm not so concerned about the regex, more matching up the price with the relevant delivery (free or charged) in the array... Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 16, 2011 Share Posted February 16, 2011 Thanks for your reply, I'm not so concerned about the regex, more matching up the price with the relevant delivery (free or charged) in the array... That's what that regex does. Quote Link to comment Share on other sites More sharing options...
shaunie Posted February 16, 2011 Author Share Posted February 16, 2011 Hi, Sorry, what I meant was how can I make sure that the array elements match up. Currently the product price is matched with the delivery price, but if the delivery is free (because the HTML is different), the rest don't match up because the delivery array is smaller... Quote Link to comment Share on other sites More sharing options...
shaunie Posted February 17, 2011 Author Share Posted February 17, 2011 Hi, After taking some time to think I realised you did answer my question after all!! Many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.