Last active
August 29, 2015 14:16
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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