Question

i am using the code bellow to get some data from an html with php simple html dom parser. almost everything works great... the issue that i am facing is that i cant grab img src... my code is:

foreach($html->find('article') as $article) {
$item['title']   = $article->find('.post-title', 0)->plaintext;
$item['thumb']   = $article->find('.post-thumbnail', 0)->plaintext;
$item['details'] = $article->find('.entry p', 0)->plaintext;

echo "<strong>img url:</strong> " . $item['thumb'];
echo "</br>";
}

My Posts structure:

<article class="item-list item_1">
<h2 class="post-title"><a href="http://localhost/mydemosite/category/sports/demo-post" title="mydemo post" rel="bookmark">my demo post 1</a></h2>
<p class="post-meta">
<span class="tie-date">2 mins ago</span>    
<span class="post-comments">
<a href="http://localhost/mydemosite/category/sports/demo-post/#disqus_thread" title="my demo post 1" data-disqus-identifier="1 http://localhost/mydemosite/category/sports/?p=1"></a></span>
</p>
<div class="post-thumbnail">
<a href="http://localhost/mydemosite/category/sports/demo-post/" title="my demo post 1" rel="bookmark">
<img width="300" height="160" src="http://localhost/mydemosite/wp-content/uploads/demo-post-300x160.jpg" class="attachment-tie-large wp-post-image" alt="my demo post 1">
</a>
</div>
<!-- post-thumbnail /-->
<div class="entry">
<p>Hello world... this is a demo post description, so if you want to read more...</p>
<a class="more-link" href="http://localhost/mydemosite/category/sports/demo-post">Read More »</a>
</div>
<div class="clear"></div>
</article>
Was it helpful?

Solution

When you use .post-thumbnail you are getting the div element.

To get the src of the img element, use this:

$item['imgurl']  = $article->find('.post-thumbnail img', 0)->src;

I added the img selector and outputing the src directly into the variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top