Matching an XHTML/XML tag with a certain attribute value


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function get_tag( $attr, $value, $xml, $tag=null ) {
  2. if( is_null($tag) )
  3. $tag = '\w+';
  4. else
  5. $tag = preg_quote($tag);
  6.  
  7. $attr = preg_quote($attr);
  8. $value = preg_quote($value);
  9.  
  10. $tag_regex = "/<(".$tag.")[^>]*$attr\s*=\s*".
  11. "(['\"])$value\\2[^>]*>(.*?)<\/\\1>/"
  12.  
  13. preg_match_all($tag_regex,
  14. $xml,
  15. $matches,
  16. PREG_PATTERN_ORDER);
  17.  
  18. return $matches[3];
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.