PHP – Get Elements Attribute form HTML

This php function helps you to get the required attributes from the elements to a given HTML.

Code Example:

<?php
function getHtmlElementsAttribute($html){
	$doc = new DOMDocument();
	@$doc->loadHTML($html);
	
	$tags = $doc->getElementsByTagName('link');
	foreach ($tags as $tag){
		echo $tag->getAttribute('href');
	}
	
	$tags = $doc->getElementsByTagName('img');
	foreach ($tags as $tag){
		echo $tag->getAttribute('src');
	}
}

$html = "<html><link href='http://www.phpmoot.com/test.css'></link><img src='http://www.phpmoot.com/image1.jpg' /><img src='http://www.phpmoot.com/image2.jpg' /></html>";
getHtmlElementsAttribute($html);
?>

Output:

http://www.phpmoot.com/test.css
http://www.phpmoot.com/image1.jpg
http://www.phpmoot.com/image2.jpg

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail