Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:在特定div中查找url图像_Php_Html_Regex_Image - Fatal编程技术网

php:在特定div中查找url图像

php:在特定div中查找url图像,php,html,regex,image,Php,Html,Regex,Image,我有一段代码: if (!$thumbdone) { if (intval($fb_image_use_content)==1) { $imgreg = '/<img .*src=["\']([^ ^"^\']*)["\']/'; preg_match_all($imgreg, trim($post->post_content), $matches); if (isset($matches[1][0])) {

我有一段代码:

if (!$thumbdone) {
    if (intval($fb_image_use_content)==1) {
        $imgreg = '/<img .*src=["\']([^ ^"^\']*)["\']/';
        preg_match_all($imgreg, trim($post->post_content), $matches);
        if (isset($matches[1][0])) {
            //There's an image on the content
            $image=$matches[1][0];
            $pos = strpos($image, site_url());
            if ($pos === false) {
                if (stristr($image, 'http://') || stristr($image, 'https://')) {
                    //Complete URL - offsite
                    $fb_image=$image;
                } else {
                    $fb_image=site_url().$image;
                }
            } else {
                //Complete URL - onsite
                $fb_image=$image;
            }
            $thumbdone=true;
        }
    }
}
if(!$thumbdone){
if(intval($fb\u图像\u使用\u内容)==1){
$imgreg='/post_content),$matches);
如果(isset($matches[1][0])){
//内容上有一个图像
$image=$matches[1][0];
$pos=strpos($image,site_url());
如果($pos==false){
if(stristr($image,'http://')| | stristr($image,'https://')){
//完整URL-异地
$fb_image=$image;
}否则{
$fb_image=site_url().$image;
}
}否则{
//完整的网址-现场
$fb_image=$image;
}
$thumbdone=true;
}
}
}
查找代码中的第一个图像。 问题是我想在div中找到第一个图像,其结构如下:

<div style="display:block" class="ogimage">
<img class="aligncenter wp-image-1030 size-full" src="http:/site.com/image.jpg" alt="image" width="600" height="315">
</div>

我在谷歌上搜索到:

我也试过了


但什么都没有。帮助?

您可以像这样使用
分解

$html = '<div style="display:block" class="ogimage"><img class="aligncenter wp-image-1030 size-full" src="http:/site.com/image.jpg" alt="image" width="600" height="315"></div>';
$result = explode($html)[7];
$html='';
$result=explode($html)[7];
使用作业的权限,而不是尝试使用正则表达式解析HTML

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);
$img   = $xpath->query('//div[@class="ogimage"]/img');

echo $img->item(0)->getAttribute('src');

您好,div位于一个复杂的页面中。我需要编辑第一个代码。这是wordpress插件的一部分。你期望的输出是什么?要提取完整的图像标记或仅提取div标记内图像的
src
,请搜索PHP Xpath。您可以获得如下第一个图像源:
//img[1]/@src
//div[@class=“ogimage”]/img[1]/@src
Tnx以获得帮助,但我需要regex:(不行?为什么需要regex?