Jeenal Infotech

How to get HTML element in PHP

Sometime we need to parse an HTML document in PHP. First thing come in ones mind is regular expression, since they are good to finding the pattern in string. Nevertheless, the reality is that HTML document can be little difficult so finding the pattern in HTML document will be difficult too.

But don’t worry we have good news 🙂 – there is already a PHP library that is made for parsing HTML.

//Download below php library file from

// Import Library

// Create DOM from URL or file
$html = file_get_html('');

// Find all videos
foreach($html->find('video') as $element)
echo $element->src . '';

// Find all images
foreach($html->find('img') as $element)
echo $element->src . '';

// Find all links
foreach($html->find('a') as $element)
echo $element->href . '';


//Didn't find <video> tag on this page 

//<img> tag result

//<a> tag result

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons