How To Extract HTML Tags And Their Attributes With PHP

There are several ways to extract specific tags from an HTML document. The one that most people will think of first is probably regular expressions. However, this is not always – or, as some would insist, ever – the best approach. Regular expressions can be handy for small hacks, but using a real HTML parser will usually lead to simpler and more robust code. Complex queries, like “find all rows with the class .foo of the second table of this document and return all links contained in those rows”, can also be done much easier with a decent parser.

There are some (though very few they may be) edge case where regular expressions might work better, so I will discuss both approaches in this post.

Extracting Tags With DOM

PHP 5 comes with a usable DOM API built-in that you can use to parse and manipulate (X)HTML documents. For example, here’s how you could use it to extract all link URLs from a HTML file :

//Load the HTML page
$html = file_get_contents('page.htm');
//Create a new DOM document
$dom = new DOMDocument;

//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn't valid XHTML.
@$dom->loadHTML($html);

//Get all links. You could also use any other tag name here,
//like 'img' or 'table', to extract other tags.
$links = $dom->getElementsByTagName('a');

//Iterate over the extracted links and display their URLs
foreach ($links as $link){
	//Extract and show the "href" attribute. 
	echo $link->getAttribute('href'), '<br>';
}

In addition to getElementsByTagName() you can also use $dom->getElementById() to find tags with a specific id. For more complex tasks, like extracting deeply nested tags, XPath is probably the way to go. For example, to find all list items with the class “foo” containing links with the class “bar” and display the link URLs :

//Load the HTML page
$html = file_get_contents('page.htm');
//Parse it. Here we use loadHTML as a static method
//to parse the HTML and create the DOM object in one go.
@$dom = DOMDocument::loadHTML($html);

//Init the XPath object
$xpath = new DOMXpath($dom);

//Query the DOM
$links = $xpath->query( '//li[contains(@class, "foo")]//a[@class = "bar"]' );

//Display the results as in the previous example
foreach($links as $link){
	echo $link->getAttribute('href'), '<br>';
}

For more information about DOM and XPath see these resources :

Honourable mention : Simple HTML DOM Parser is a popular alternative HTML parser for PHP 5 that lets you manipulate HTML pages with jQuery-like ease. However, I personally wouldn’t recommend using it if you care about your script’s performance, as in my tests Simple HTML DOM was about 30 times slower than DOMDocument.

Extracting Tags & Attributes With Regular Expressions

There are only two advantages to processing HTML with regular expressions – availability and edge-case performance. While most parsers require PHP 5 or later, regular expressions are available pretty much anywhere. Also, they are a little bit faster than real parsers when you need to extract something from a very large document (on the order of 400 KB or more). Still, in most cases you’re better off using the PHP DOM extension or even Simple HTML DOM, not messing with convoluted regexps.

That said, here’s a PHP function that can extract any HTML tags and their attributes from a given string :

/**
 * extract_tags()
 * Extract specific HTML tags and their attributes from a string.
 *
 * You can either specify one tag, an array of tag names, or a regular expression that matches the tag name(s). 
 * If multiple tags are specified you must also set the $selfclosing parameter and it must be the same for 
 * all specified tags (so you can't extract both normal and self-closing tags in one go).
 * 
 * The function returns a numerically indexed array of extracted tags. Each entry is an associative array
 * with these keys :
 * 	tag_name	- the name of the extracted tag, e.g. "a" or "img".
 *	offset		- the numberic offset of the first character of the tag within the HTML source.
 *	contents	- the inner HTML of the tag. This is always empty for self-closing tags.
 *	attributes	- a name -> value array of the tag's attributes, or an empty array if the tag has none.
 *	full_tag	- the entire matched tag, e.g. '<a href="http://example.com">example.com</a>'. This key 
 *		          will only be present if you set $return_the_entire_tag to true.	   
 *
 * @param string $html The HTML code to search for tags.
 * @param string|array $tag The tag(s) to extract.							 
 * @param bool $selfclosing	Whether the tag is self-closing or not. Setting it to null will force the script to try and make an educated guess. 
 * @param bool $return_the_entire_tag Return the entire matched tag in 'full_tag' key of the results array.  
 * @param string $charset The character set of the HTML code. Defaults to ISO-8859-1.
 *
 * @return array An array of extracted tags, or an empty array if no matching tags were found. 
 */
function extract_tags( $html, $tag, $selfclosing = null, $return_the_entire_tag = false, $charset = 'ISO-8859-1' ){
	
	if ( is_array($tag) ){
		$tag = implode('|', $tag);
	}
	
	//If the user didn't specify if $tag is a self-closing tag we try to auto-detect it
	//by checking against a list of known self-closing tags.
	$selfclosing_tags = array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta', 'col', 'param' );
	if ( is_null($selfclosing) ){
		$selfclosing = in_array( $tag, $selfclosing_tags );
	}
	
	//The regexp is different for normal and self-closing tags because I can't figure out 
	//how to make a sufficiently robust unified one.
	if ( $selfclosing ){
		$tag_pattern = 
			'@<(?P<tag>'.$tag.')			# <tag
			(?P<attributes>\s[^>]+)?		# attributes, if any
			\s*/?>					# /> or just >, being lenient here 
			@xsi';
	} else {
		$tag_pattern = 
			'@<(?P<tag>'.$tag.')			# <tag
			(?P<attributes>\s[^>]+)?		# attributes, if any
			\s*>					# >
			(?P<contents>.*?)			# tag contents
			</(?P=tag)>				# the closing </tag>
			@xsi';
	}
	
	$attribute_pattern = 
		'@
		(?P<name>\w+)							# attribute name
		\s*=\s*
		(
			(?P<quote>[\"\'])(?P<value_quoted>.*?)(?P=quote)	# a quoted value
			|							# or
			(?P<value_unquoted>[^\s"\']+?)(?:\s+|$)			# an unquoted value (terminated by whitespace or EOF) 
		)
		@xsi';

	//Find all tags 
	if ( !preg_match_all($tag_pattern, $html, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ){
		//Return an empty array if we didn't find anything
		return array();
	}
	
	$tags = array();
	foreach ($matches as $match){
		
		//Parse tag attributes, if any
		$attributes = array();
		if ( !empty($match['attributes'][0]) ){ 
			
			if ( preg_match_all( $attribute_pattern, $match['attributes'][0], $attribute_data, PREG_SET_ORDER ) ){
				//Turn the attribute data into a name->value array
				foreach($attribute_data as $attr){
					if( !empty($attr['value_quoted']) ){
						$value = $attr['value_quoted'];
					} else if( !empty($attr['value_unquoted']) ){
						$value = $attr['value_unquoted'];
					} else {
						$value = '';
					}
					
					//Passing the value through html_entity_decode is handy when you want
					//to extract link URLs or something like that. You might want to remove
					//or modify this call if it doesn't fit your situation.
					$value = html_entity_decode( $value, ENT_QUOTES, $charset );
					
					$attributes[$attr['name']] = $value;
				}
			}
			
		}
		
		$tag = array(
			'tag_name' => $match['tag'][0],
			'offset' => $match[0][1], 
			'contents' => !empty($match['contents'])?$match['contents'][0]:'', //empty for self-closing tags
			'attributes' => $attributes, 
		);
		if ( $return_the_entire_tag ){
			$tag['full_tag'] = $match[0][0]; 			
		}
		 
		$tags[] = $tag;
	}
	
	return $tags;
}

Usage examples

Extract all links and output their URLs :

$html = file_get_contents( 'example.html' );
$nodes = extract_tags( $html, 'a' );
foreach($nodes as $link){
	echo $link['attributes']['href'] , '<br>';
}

Extract all heading tags and output their text :

$nodes = extract_tags( $html, 'h\d+', false );
foreach($nodes as $node){
	echo strip_tags($link['contents']) , '<br>';
}

Extract meta tags :

$nodes = extract_tags( $html, 'meta' );

Extract bold and italicised text fragments :

$nodes = extract_tags( $html, array('b', 'strong', 'em', 'i') );
foreach($nodes as $node){
	echo strip_tags( $node['contents'] ), '<br>';
}

The function is pretty well documented, so check the source if anything is unclear. Of course, you can also leave a comment if you have any further questions or feedback.

Related posts :

30 Responses to “How To Extract HTML Tags And Their Attributes With PHP”

  1. Jānis Elsts says:

    It doesn’t work because that page contains a commented-out title tag:

    <!--<title> |</title>-->

    The regex-based extraction function doesn’t know to ignore HTML comments, so it assumes that title tag is what you’re looking for and returns it.

    Solution: Use the DOM API instead.

  2. ivo says:

    It doesn’t work nothing

  3. it works as expected.

  4. TommyWillB says:

    It doesn’t seem to get everything.

    I tried using this to dump a list of all of the DIV ID’s and CLASS’s, but it’s skipping some:

    <?
    require_once("./regexp_parser.php");

    $html = file_get_contents( 'http://www.thought-matrix.com&#039; );
    $nodes = extract_tags( $html, 'div' );

    echo '’;
    echo “TAGidclassname”;

    foreach($nodes as $div){
    echo “DIV” . $div['attributes']['id'] . ” ” . $div['attributes']['class'] . ” ” . $div['attributes']['name'] . ” “;
    }

    echo ”;
    ?>

    The output looks like this:

    DIV wrap
    DIV header-nav nav
    DIV main
    DIV desc-web-design description
    DIV desc-ecommerce description

    But if you view source of the page, you’ll see that there DIV’s between main and desc-web-design:



    Why did it skip some?

  5. TommyWillB says:

    Code:

    <?
    require_once(“./regexp_parser.php”);

    $html = file_get_contents( ‘http://www.thought-matrix.com’ );
    $nodes = extract_tags( $html, ‘div’ );

    echo ‘<table border=”1″>’;
    echo “</tr><th>TAG</th><th>id</th><th>class</th><th>name</th><tr>”;

    foreach($nodes as $div){
    echo “</tr><td>DIV</td><td>” . $div['attributes']['id'] . ” </td><td>” . $div['attributes']['class'] . ” </td><td>” . $div['attributes']['name'] . ” </td><tr>”;
    }

    echo ‘</table>’;
    ?>

    HTML:

    <div id=”main”>
    <div id=”description-container”>

    <div class=”description” id=”desc-content-management”>
    <h1>Content Management</h1>
    <p>You can never have too much<br /> content if you know how to harness it.</p>
    <div class=”desc-more”><a href=”/solutions/website-content-management-systems”>Learn More »</a></div>
    </div>

    <div class=”description” id=”desc-web-design”>
    <h1>Web<br /> Design</h1>
    <p>With award-winning design,<br /> you’ll have them at hello.</p>
    <div class=”desc-more”><a href=”/solutions/website-design-development”>Learn More »</a></div>
    </div>

  6. Jānis Elsts says:

    I’m not sure what could cause this problem, but I would strongly recommend using the DOM API to parse HTML. It’s much more reliable than parsing with regular expressions.

  7. TommyWillB says:

    I agree… I just wanted folks to know that this RegEx was suspect before using it.

  8. Within this Sold Out After Crisis Review and commentary, you will find out about a group of guides that
    may put together you for virtually any disaster that
    might be headed our way, no matter if it’s
    a hurricane, a flood, a terrorist assault, planet war III, or an increasingly extra very likely economic collapse.

    Damien Campbell is actually a survival qualified who has put in several years learning
    survival matters such as long-term foods storage, drinking water purification tactics, option sources of strength, crisis medicine, and
    yard gardening. When he began penning this guidebook, he started by creating a checklist of meals to stock up on.
    The trouble was that record was fairly long, and a few of
    the food items ended up hard to keep. While in the
    interest of retaining his guidebook as simple as attainable, he narrowed the last all the way down
    to 37 essential meals merchandise that everyone purchase now ahead of it’s far too late.

    Sadly, the normal American has no idea how lousy points really are.
    The media would really like to faux all the things is okay, but in fact, we are facing additional danger now than we have in
    many years. Farmers are functioning beyond acreage, aged sources
    of drinking drinking water are drying up, terrorists are
    more common than ever, and deficit investing is draining our financial system
    and could easily result in hyperinflation.

    Which is why it is so significant for being prepared. Should you hold out until the crisis is at your doorstep, will probably be way too late for the reason that by then,
    the public may have emptied the stores. A lot of individuals do not know this, but grocery outlets only
    retain 3-4 times really worth of food available.
    And when disasters strike, that food stuff is gone in only 3-4 hours!
    It is happened repeatedly before.

    My blog; 37 Food Items Sold Out

  9. Muchas gracias, me ha servido para un trabajo que tengo que entregar en el colegio. Besos!

Leave a Reply