使用DOM PHP获取特定图像的src属性

使用DOM PHP获取特定图像的src属性,php,dom,facebook-opengraph,Php,Dom,Facebook Opengraph,我正在尝试使用PHP中的jQuery函数(FB Open Graph不执行JS代码,所以它必须在服务器端执行): captureurl=jQuery('.blog content').find('img').attr('src'); jQuery('head')。追加(“”); 我看到我可以得到这样的图像属性: <?php doc = new DOMDocument(); $doc->loadHTMLFile($url); $xpath = new DOMXpath($doc);

我正在尝试使用PHP中的jQuery函数(FB Open Graph不执行JS代码,所以它必须在服务器端执行):

captureurl=jQuery('.blog content').find('img').attr('src');
jQuery('head')。追加(“”);
我看到我可以得到这样的图像属性:

<?php doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$imgs = $xpath->query("//img");
for ($i=0; $i < $imgs->length; $i++) {
    $img = $imgs->item($i);
    $src = $img->getAttribute("src");
    // do something with $src
} ?>

但是如何将div中的第一个图像src与.blog内容类结合起来呢


感谢您的帮助:)

用以下内容替换
$xpath->query(“//img”)

$imgs = $xpath->query('//img[contains(attribute::class, "blog-content")]'); //here we are querying domdocument to find img which has class .blog-content

您可能的副本也应添加解释。这样,OP(以及其他未来的读者)就更容易使用它并对其进行更改,以适应他们在其他情况下的需要。
$imgs = $xpath->query('//img[contains(attribute::class, "blog-content")]'); //here we are querying domdocument to find img which has class .blog-content