Return each div with a certain class name in PHP

Laim McKenzie

OK, So I have a page that has that has images on it that I'm looking to scrape and return the following information:

  • Base Image URL ("website.com/imagepage")
  • Image URL ("website.com/image.png")
  • Image QUOTE if it has one ("Wow, nice image")

I have it working to return ONE Image, but I need it to return all of them (there is about 5)

This is what I have at the moment:

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];
}

This is what it returns

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

The give images have different ID's (the 9384938 part).

How would I get the information needed from what it returns?

I have another function at the moment that returns the data for one of the images (kind of), but it's basically just the exact same thing with code between the explode, which is very messy.

trincot

You could use PHP's DOMDocument class with this function:

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

Call it as:

$result = getDataFromHTML($html);

Output for the sample data is:

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

The outer array would have more such entries if run on a HTML string that has several of those DOM structures.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

php - if/else if function based on the id name of a div that has a certain class name?

PHP for each Loop return only certain elements

jQuery: Check if div with certain class name exists

Scroll a div to a certain class name horizontally

How to apply a function that affects the parent of each element with a certain class name?

Extract all p and a tags from each div with a certain class

Find each div by class name in a div based on a DOM objects location

jQuery- Can't Add div with certain class name

Get elements by class name in a certain div through Javascript

Find next Div Class name after parent div class name, using php/dom/xpath?

Python Scrapy unable to get each class name with its value from each div

Move each div of a certain class from the start to the end of it's parent using jQuery

jsoup remove div with a certain class

Finding div by class name

Add class to parent of <img> with certain class name or certain source

return name of the array that contains certain key with jq

php return $this in class method

PHP Return Static Class

php class return array

PHP class return nothing

How to to create groups of php arrays encoded as json and return each group by name

Target each class and return a string and class

Change Div Class On Each Click

PHP SimpleHTMLDOM : how to get value from div by class name inside of another div?

JS alter element with certain id and class name

how to give a certain td a class name with javascript?

How to call certain methods and return certain objects based on class type

jQuery if div has certain text in certain div, hide FIRST div with certain class

Move each div with same class into another div