-1

I'm trying to get all images urls from a html-string with php. Both from img-tags and from inline css (background-image)

<?php
$html = '
<div style="background-image : url(https://exampel.com/media/logo.svg);"></div>
<img src="https://exampel.com/media/my-photo.jpg" />
<div style="background-image:url('https://exampel.com/media/icon.png');"></div>
';


preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>|background-image[ ]?:[ ]?url\([ ]?[\']?["]?(.*?\.(?:png|jpg|jpeg|gif|svg))/i', $html, $image);
echo('<pre>'.print_r($image, true).'</pre>');
?>

The output from this is:

Array
(
    [0] => background-image : url(https://exampel.com/media/logo.svg
    [src] => 
    [1] => 
    [2] => https://exampel.com/media/logo.svg
)

Prefered output would be:

Array
(
    [0] => https://exampel.com/media/logo.svg
    [1] => https://exampel.com/media/my-photo.jpg
    [2] => https://exampel.com/media/icon.png
)

I'm missing something here but I cant figure out what

Dennis
  • 3
  • 1
  • [Relevant](https://stackoverflow.com/a/1732454/6632744) – Joundill Jan 20 '22 at 22:02
  • Does this answer your question? [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Ryszard Czech Jan 20 '22 at 23:02
  • You could also just use [DOMDocument](https://www.php.net) instead of the unnecessary headache and inaccuracies. – Sherif Jan 21 '22 at 00:01

1 Answers1

0

Use preg_match_all() and rearrange your result:

<?php
$html = <<<EOT
<div style="background-image : url(https://exampel.com/media/logo.svg);"></div>
<img src="https://exampel.com/media/my-photo.jpg" />
<div style="background-image:url('https://exampel.com/media/icon.png');"></div>
EOT;

preg_match_all(
    '/<img.+src=[\'"](.+?)[\'"].*>|background-image ?: ?url\([\'" ]?(.*?\.(?:png|jpg|jpeg|gif|svg))/i',
    $html,
    $matches,
    PREG_SET_ORDER
);

$image = [];
foreach ($matches as $set) {
    unset($set[0]);
    foreach ($set as $url) {
        if ($url) {
            $image[] = $url;
        }
    }
}

echo '<pre>' . print_r($image, true) . '</pre>' . PHP_EOL;
took
  • 172
  • 6