Monday 6 November 2017

php - Get all images and return the src





My code bellow grabs some content. in this content there are some
photos.
How can i loop through this content find all images and return their
src?



my code so
far:



$items =
$html->find('div.post-single-content',0)->children(1)->outertext;
foreach($items
$node) {
$node->find('img');
}
print_r
($node);

class="post-text" itemprop="text">
class="normal">Answer



Don't use
regex, use a parser.
Example:



$string = '            src="You want this" style="width:200px;" />';
$doc = new
DOMDocument();
$doc->loadHTML($string);
$images =
$doc->getElementsByTagName('img');
foreach ($images as $image) {

echo $image->getAttribute('src') .
"\n";
}


Output:





You want this




No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...