- preg_match: documentation ( source)
- print_r: documentation ( source)
- libxml_clear_errors: documentation ( source)
- trim: documentation ( source)
- libxml_use_internal_errors: documentation ( source)
<?php
$html = '
<html><head></head><body>
<div class="single-post-image" style="background-image:url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg )"></div>
<div class="single-post-image" style="background-image: url( https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(\'https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg\')"></div>
<div class="single-post-image"></div>
</body>
</html>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DomXPath($dom);
$images = [];
foreach ($xpath->query("//*[contains(@class, 'single-post-image')]") as $img) {
if ($img->hasAttribute('style')) {
preg_match('/url\((.*)\)/', $img->getAttribute('style'), $match);
if (isset($match[1])) $images[] = trim($match[1], '\'" ');
}
}
print_r($images);