Skip to content

Instantly share code, notes, and snippets.

@rolandinsh
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolandinsh/2cd41b671f1ac845f309 to your computer and use it in GitHub Desktop.
Save rolandinsh/2cd41b671f1ac845f309 to your computer and use it in GitHub Desktop.
Matching an HTML (or XML) tag with a certain attribute value (e.g. class or tag) by Mike Malone (https://twitter.com/mjmalone)
function get_tag( $attr, $value, $xml, $tag=null ) {
if( is_null($tag) )
$tag = '\\w+';
else
$tag = preg_quote($tag);
$attr = preg_quote($attr);
$value = preg_quote($value);
$tag_regex = "/<(".$tag.")[^>]*$attr\\s*=\\s*".
"(['\\"])$value\\\\2[^>]*>(.*?)<\\/\\\\1>/"
preg_match_all($tag_regex,
$xml,
$matches,
PREG_PATTERN_ORDER);
return $matches[3];
}
// {<tag[^>]*attribute\\s*=\\s*(["'])value\\\\1[^>]*>(.*?)</tag>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment