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
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:
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
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:
Both functions return the text method in an ordered list.