用PHP中的特定类名返回每个div

用PHP中的特定类名返回每个div,php,html,Php,Html,好的,我有一个页面,上面有一些图片,我想从中提取并返回以下信息: 基本图像URL(“website.com/imagepage”) 图像URL(“website.com/Image.png”) 如果有图像引用(“哇,好图像”) 我有它的工作,以返回一个图像,但我需要它返回所有他们(有大约5) 这就是我目前的情况: function getMostRecentScreenshot($url) { $content = file_get_contents($url); $first_ste

好的,我有一个页面,上面有一些图片,我想从中提取并返回以下信息:

  • 基本图像URL(“website.com/imagepage”)
  • 图像URL(“website.com/Image.png”)
  • 如果有图像引用(“哇,好图像”)
我有它的工作,以返回一个图像,但我需要它返回所有他们(有大约5)

这就是我目前的情况:

function getMostRecentScreenshot($url) {
 $content = file_get_contents($url);

 $first_step = explode('<div class="imageWall5Floaters">' , $content );
 $second_step = explode('<div style="clear: left;"></div>' , $first_step[1] );

 return $second_step[0];
}
函数getMostRecentScreenshot($url){
$content=file\u get\u contents($url);
$first_step=分解(“”,$content);
$second_step=爆炸(“”,$first_step[1]);
返回$second_step[0];
}
这就是它的回报

<div class="floatHelp">
<a href="websiteurl.com/imagepage" onclick="return OnScreenshotClicked(9384938);" class="profile_media_item modalContentLink  " data-desired-aspect="1.77777777778">
    <div style="background-image: url('website.com/image');" class="imgWallItem  " id="imgWallItem_757249198">
        <div style="position: relative;">
            <input type="checkbox" style="position: absolute; display: none;" name="screenshots[9384938]" class="screenshot_checkbox" id="screenshot_checkbox_9384938" />
        </div>
        <div class="imgWallHover" id="imgWallHover9384938">
            <div class="imgWallHoverBottom">
                <div class="imgWallHoverDescription ">
                    <q class="ellipsis">Quote about the image</q>
                </div>
            </div>
        </div>


    </div>
</a>

给定图像具有不同的ID(9384938零件)

我如何从它返回的内容中获得所需的信息


我现在有另一个函数,它返回其中一个图像的数据(有点),但基本上与分解之间的代码完全相同,这非常混乱。

您可以使用PHP的
DOMDocument
类来实现此函数:

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

    foreach($doc->getElementsByTagName('a') as $a) {
        if (strpos($a->getAttribute('class'), 'profile_media_item') !== false) {
            $row = [];
            $row['baseURL'] = $a->getAttribute('href');
            foreach($a->getElementsByTagName('div') as $div) {
                preg_match("~(?<=url\(['\"]).*?(?=['\"])~", 
                           $div->getAttribute('style'), $attr);
                $row['imageURL'] = reset($attr);
                foreach($a->getElementsByTagName('q') as $q) {
                    $row['quote'] = $q->textContent;
                    break;
                }
                break;
            }
            $result[] = $row;
        }
    }
    return $result;
}
样本数据的输出为:

array (
  array (
    'baseURL' => 'websiteurl.com/imagepage',
    'imageURL' => 'website.com/image',
    'quote' => 'Quote about the image'
  )
)

如果在具有多个DOM结构的HTML字符串上运行,外部数组将有更多这样的条目。

您可以将PHP的
DOMDocument
类用于此函数:

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

    foreach($doc->getElementsByTagName('a') as $a) {
        if (strpos($a->getAttribute('class'), 'profile_media_item') !== false) {
            $row = [];
            $row['baseURL'] = $a->getAttribute('href');
            foreach($a->getElementsByTagName('div') as $div) {
                preg_match("~(?<=url\(['\"]).*?(?=['\"])~", 
                           $div->getAttribute('style'), $attr);
                $row['imageURL'] = reset($attr);
                foreach($a->getElementsByTagName('q') as $q) {
                    $row['quote'] = $q->textContent;
                    break;
                }
                break;
            }
            $result[] = $row;
        }
    }
    return $result;
}
样本数据的输出为:

array (
  array (
    'baseURL' => 'websiteurl.com/imagepage',
    'imageURL' => 'website.com/image',
    'quote' => 'Quote about the image'
  )
)

如果在具有多个DOM结构的HTML字符串上运行,外部数组将有更多这样的条目。

您会发现它很有用:我完全忘记了simplehtmldom的存在。谢谢你@KostasMitsarakis!您需要使用正则表达式来提取所需内容。您会发现它很有用:我完全忘记了simplehtmldom的存在。谢谢你@KostasMitsarakis!您需要使用正则表达式来提取所需内容。这非常有效,谢谢!唯一的问题是,它没有返回imageURL?在编辑之前,它返回一个的所有内容,但是编辑现在返回所有内容,不包括图像?更正:我不小心删除了第二个
break
这非常有效,谢谢!唯一的问题是,它没有返回imageURL?在编辑之前,它返回一个的所有内容,但是编辑现在返回所有内容,不包括图像?更正:我不小心删除了第二个
break