繁体   English   中英

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

[英]Get src attribute of specific image with DOM PHP

我正在尝试执行与PHP中的jQuery函数相同的功能(FB Open Graph不执行JS代码,因此必须在服务器端执行):

    <script>captureurl=jQuery('.blog-content').find('img').attr('src');
jQuery('head').append("<meta property='og:image' content="+captureurl+"/></meta>");</script>

我已经看到我可以得到这样的图像属性:

<?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
} ?>

但是,如何使用.blog-content类定位div中的第一个图像src?

谢谢你的帮助 :)

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM