Ferrmolina
Ferrmolina

Reputation: 2771

Get SRC from IMG with Class, using DOM php

I'm trying to get the src from this a:

<a class="poster" href="#">
<img itemprop="image" id="upload_poster" alt="alt" title="title" class="shadow"  
src="https://image.tmdb.org/t/p/w185/z99yU71wH7JBHZX4tQ3XzPG521M.jpg"/>    
</a>    

Can any help me? I've this:

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

$xpath = new DOMXPath($doc);
$tags = $xpath->query('//img[@class="shadow"]');
foreach ($tags as $tag) {
   $tag->getAttribute('src');
}

//Guardo todo en un array
$data = array('moviedb' => $tag, 'msg' => 'success');

I don't want echo, i need to save the data in a array.

This return: [Object][Object]

Upvotes: 0

Views: 155

Answers (1)

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47169

In your loop you need to include the data that will populate the array:

foreach ($tags as $tag) {
   $src = $tag->getAttribute('src');
   $data[] = array('moviedb' => $src, 'msg' => 'success');
}

The result would then look like:

Array
(
    [0] => Array
        (
            [moviedb] => https://image.tmdb.org/t/p/w185/z99yU71wH7JBHZX4tQ3XzPG521M.jpg
            [msg] => success
        )
)

Upvotes: 1

Related Questions