0

我正在尝试修改我在此处找到的一些 PHP 代码,以使其从任何 url 获取 og:image url,标签通常是这样的(当一个不存在时,我会做一个 isset)

<meta property="og:image" content="https://MYGREATOTHERDOMAIN.com/images/rock.jpg" />

或者可能是

<meta name="og:image" content="https://images.MYGREATDOMAIN.co.uk/img.jpg"/>

我正在使用的代码..

$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>"; 

这现在正在提取所有元标记中的所有“内容”,我需要它只搜索并返回 og:image url,但不知道如何做到这一点,非常感谢任何帮助。

编辑添加:我可能不需要数组,因为我只会返回 1 个输出 url,我只是从示例代码开始,认为这是一个很好的起点并适应它,但已经卡住了。再次感谢。

如果我们在https://www.bbcgoodfood.com/recipes/classic-sausage-mash上运行上述内容,我得到的输出是..

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
)
4

0 回答 0