How to extract img src and alt from html using PHP? Last Updated : 15 May, 2019 Comments Improve Suggest changes Like Article Like Report Extraction of image attributes like 'src', 'alt', 'height', 'width' etc from a HTML page using PHP. This task can be done using the following steps. Loading HTML content in a variable(DOM variable). Selecting each image in that document. Selecting attribute and save it's content to a variable. Output as HTML img object or as plain values as required. Example 1: This example displays the image object as output. php <?php // error_reporting(0); function crawl_page($url) { $dom = new DOMDocument('1.0'); // Loading HTML content in $dom @$dom->loadHTMLFile($url); // Selecting all image i.e. img tag object $anchors = $dom -> getElementsByTagName('img'); // Extracting attribute from each object foreach ($anchors as $element) { // Extracting value of src attribute of // the current image object $src = $element -> getAttribute('src'); // Extracting value of alt attribute of // the current image object $alt = $element -> getAttribute('alt'); // Extracting value of height attribute // of the current image object $height = $element -> getAttribute('height'); // Extracting value of width attribute of // the current image object $width = $element -> getAttribute('width'); // Given Output as image with extracted attribute, // you can print value of those attributes also echo '<img src="'.$src.'" alt="'.$alt.'" height="' . $height.'" width="'.$width.'"/>'; } } crawl_page("https://www.google.com/search?q=geeksforgeeks&tbm=isch"); ?> Output: Example 2: This example displays the attribute of an image object. php <?php // error_reporting(0); function crawl_page($url) { $dom = new DOMDocument('1.0'); // Loading HTML content in $dom @$dom->loadHTMLFile($url); // Selecting all image i.e. img tag object $anchors = $dom -> getElementsByTagName('img'); // Extracting attribute from each object foreach ($anchors as $element) { // Extracting value of src attribute of // the current image object $src = $element -> getAttribute('src'); // Extracting value of alt attribute of // the current image object $alt = $element -> getAttribute('alt'); // Extracting value of height attribute // of the current image object $height = $element -> getAttribute('height'); // Extracting value of width attribute of // the current image object $width = $element -> getAttribute('width'); // Display Output as value of those attributes echo 'src='.$src.'<br> alt='.$alt.'<br> height=' . $height.'<br> width='.$width.'<hr>'; } } crawl_page("https://www.google.com/search?q=flowers&tbm=isch"); ?> Output: Create Quiz Comment G gekcho Follow 0 Improve G gekcho Follow 0 Improve Article Tags : Web Technologies PHP PHP Programs Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like