How to Extract Img SRC, Title and Alt from HTML in PHP?
Hello Guys,
Today, I will let you know example of php get image tag from html. I explained simply about how to get src value from img tag in php. I would like to show you how to get img src from html in php. It's a simple example of how to find img src from html in php. So, let us see in detail an example.
In this example, we will use file_get_contents(), DOMDocument() and getAttribute() function to get img src value from html content in php. let's see the simple php example with output:
index.php
<?php
$url = "https://www.example.com";
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
echo "<br/><br/>SRC:" . $tag->getAttribute('src');
echo "<br/>ALT:" . $tag->getAttribute('alt');
echo "<br/>Title:" . $tag->getAttribute('title');
}
?>
Output:
I hope it can help you...