RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Example PHP Functions to Return Recipe Methods From Cooking Websites

These two functions are to support the basic Food2fork recipe shortcode provided here. The purpose of our functions is to scrape the destination cooking website for cooking methods (your own of course - don't steal). They're very basic and far from ideal given their reliance on regular expressions to parse the HTML.

We've provided just two examples: one for BBC Good Food, and other for AllRecipes.com.

Any part of the page can be parsed for appropriate content. What follows is just an example.

BBC Good Food

1
<?php 
2
/*
3
 Scrape BBC Good Food for Recipe Method
4
 http://www.beliefmedia.com/code/php-snippets/recipe-methods
5
*/
6
 
7
function beliefmedia_bbcgoodfood_method($url) {
8
 
9
  $data = @file_get_contents($url);
10
  if ($data === false) return false;
11
 
12
  preg_match_all('/
13
<li class="method__item(.*?)?"><span>(.*?)<\/span><\/li>/si', $data, $match);
14
  $method = $match['2'];
15
 
16
  if (empty($method)) return false;
17
  else foreach ($method AS $instruction) $result .= '
18
<li>' . $instruction . '</li>
19
 
20
';
21
  $return = '
22
<ol>' . $result . '</ol>
23
 
24
';
25
 
26
 return $return;
27
}

Example use:

1
echo beliefmedia_bbcgoodfood_method($url = 'https://www.bbcgoodfood.com/recipes/1153653/caramel-slice');

All Recipes

1
<?php 
2
/*
3
 Scrape All Recipes for Recipe Method
4
 http://www.beliefmedia.com/code/php-snippets/recipe-methods
5
*/
6
 
7
function beliefmedia_allrecipes_method($url) {
8
 
9
  $data = @file_get_contents($url);
10
  if ($data === false) return false;
11
 
12
  preg_match_all('/<span class="recipe-directions__list--item">(.*?)<\/span>/si', $data, $match);
13
  $method = $match['1'];
14
 
15
  if (empty($method)) return false;
16
  else foreach ($method AS $instruction) $result .= '
17
<li>' . $instruction . '</li>
18
 
19
';
20
  $return = '
21
<ol>' . $result . '</ol>
22
 
23
';
24
 
25
 return $return;
26
}

Example use:

1
echo beliefmedia_allrecipes_method($url = 'http://allrecipes.com/recipe/15004/award-winning-soft-chocolate-chip-cookies/');

Both functions return the text method in an ordered list.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment