I'm trying to adapt some PHP code I found here to make it get the og:image url from any url, The tags are usally like so (and i will make a isset for when one is not present)
<meta property="og:image" content="https://MYGREATOTHERDOMAIN.com/images/rock.jpg" />
or could be
<meta name="og:image" content="https://images.MYGREATDOMAIN.co.uk/img.jpg"/>
The code I'm using..
$htmlContent = file_get_contents($singlemeal[0][link]);
// read all og:image tags into an array
preg_match_all('/<meta[^>]+>/i',$htmlContent, $imgTags);
for ($i = 0; $i < count($imgTags[0]); $i++) {
// get the source string
preg_match('/type=og:image',$imgTags[0][$i], $imgage);
preg_match('/content="([^"]+)/i',$imgTags[0][$i], $imgage);
// remove opening 'content=' tag, can`t get the regex right
$origImageSrc[] = str_ireplace( 'content="', '', $imgage[0]);
}
// will output all your img src's within the html string
echo "<pre>";
print_r ($origImageSrc);
echo "</pre>";
This is now pulling all the 'content' found in all meta tags and I need it to only search for and return og:image url, but not sure how to make it do this, any help greatly appreciated.
Edit to add: I probably don't need an array since I will only be returning 1 output url, I simply started with the example code thinking it be a good place to start and adapt to suit but have got stuck. Thanks again.
If we run the above on say https://www.bbcgoodfood.com/recipes/classic-sausage-mash the output im getting is..
Array
(
[0] => width=device-width
[1] =>
[2] => Classic sausage & mash recipe - BBC Good Food
[3] => A classic family comfort food meal with creamy mash and golden brown sausages. Fresh seasonal veg like steamed broccoli make this meal 3 of your 5 a day
[4] => https://www.bbcgoodfood.com/recipes/classic-sausage-mash
[5] => Classic sausage & mash
[6] => BBC Good Food
[7] => A classic family comfort food meal with creamy mash and golden brown sausages. Fresh seasonal veg like steamed broccoli make this meal 3 of your 5 a day
[8] => article
[9] => https://images.immediate.co.uk/production/volatile/sites/30/2020/08/sausage-and-mash-2cb0bee.jpg
[10] => Classic sausage & mash
[11] => 400
[12] => 440
[13] => summary_large_image
[14] => BBC Good Food
[15] => @bbcgoodfood
[16] => Hkgeqt1fifrCaIrdMvoMK4wxVtQsqQPh3VY77hxXjWg
[17] => e0dc3ea18b52d6be8f7d588b29af3605
[18] => 31296838546
[19] => 37
)