Skip to content

Instantly share code, notes, and snippets.

@amphibian
Created October 18, 2009 13:12
Show Gist options
  • Save amphibian/212675 to your computer and use it in GitHub Desktop.
Save amphibian/212675 to your computer and use it in GitHub Desktop.
PHP function to return any given attribute of a complete HTML tag.
/*
This function will return any given attribute of a complete HTML tag.
Originally found at http://www.phpfreaks.com/forums/index.php?topic=209684.0
*/
function getAttribute($attribute, $tag)
{
$re = '/' . preg_quote($attribute) . '=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/is';
if( preg_match($re, $tag, $match) )
{
return urldecode($match[2]);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment