How to Extract img src and alt value From HTML using PHP?

Hello Guys i will tell you today through Experts PHP Tutorial, How You Can Extract the src value or alt value of the img tag of any HTML Page of PHP. i will tell you step by step. So let’s go.

First of all you create a php file. You can name that file according to yourself. Add the code below to your php file and run it on your local server and you will get your output.


function page($url) { 
$dom = new DOMDocument('1.0'); 
$imgget = $dom -> getElementsByTagName('img');
foreach ($imgget as $element) {
$src = $element -> getAttribute('src');
$alt = $element -> getAttribute('alt'); 
$height = $element -> getAttribute('height');
$width = $element -> getAttribute('width');
echo '<img src="'.$src.'" alt="'.$alt.'" height="'
. $height.'" width="'.$width.'"/>'; 


DOMDocument :- The PHP Document Object Model (DOM) represents your entire document through a single object. This object is a document.With the help of this object, you can access any HTML element in the entire document. DOM provides you control of all HTML elements (tags) of a web page. With its help, you can remove any element or add new elements.