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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert Array to XML in PHP?
- How to Upgrade PHP Version from 8.2 to 8.3 in Ubuntu?
- How to Install PHP 8.3 on Ubuntu 22.04?
- How to Send Mail using PHPMailer in Laravel?
- How to Get Duplicate Values from Array in PHP?
- How to Check If a Value Exists in an Array in PHP?
- PHP array_merge() Function Example
- How to Remove Numeric Keys in PHP Array?
- How to Remove String Values in PHP Array?
- How to Get First 5 Elements of Array in PHP?
- How to Get First 2 Elements of Array in PHP?
- How to Get Last 2 Elements of Array in PHP?