Search image tags inside PHP string

Asked

Viewed 340 times

3

I have a string like:

 Veja o logotipo do PHP: <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/2000px-Webysther_20160423_-_Elephpant.svg.png">

I need to pick up the image path in the string. The string can get multiple images.

How can I pick up exactly the image path and remove the rest of the string?

1 answer

3


You can do something similar to that answer from SOEN:

$doc = new DOMDocument();
$doc->loadHTML($string_com_img);
$imageTags = $doc->getElementsByTagName('img');

$srcs = [];

foreach($imageTags as $tag) {
    $srcs[] = $tag->getAttribute('src');
}

This question is also very similar to your problem. It’s worth taking a look:

Take a value that is within a <span> on another site using PHP

  • 1

    Thank you so much is just what I need! As soon as I can accept your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.