Snippets v2

Languages
My Snippets

Find matching HTML tags (greedy) Login or Register to Bookmark this snippet

Description PHP

Finds HTML tags within a string

The Code Download
  1. <?php
  2. // The 2 is an example of backreferencing. This tells pcre that
  3. // it must match the second set of parentheses in the regular expression
  4. // itself, which would be the ([w] ) in this case. The extra backslash is
  5. // required because the string is in double quotes.
  6.  
  7. $html = "<b>bold text</b><a href=howdy.html>click me</a>";
  8. preg_match_all ("/(<([w] )[^>]*>)(.*)(</2>)/", $html, $matches);
  9. for ($i=0; $i< count($matches[0]); $i++) {
  10.   echo "matched: ".$matches[0][$i]."n";
  11.   echo "part 1: ".$matches[1][$i]."n";
  12.   echo "part 2: ".$matches[3][$i]."n";
  13.   echo "part 3: ".$matches[4][$i]."nn";
  14. }
  15. ?>
Credits Contact JC

Added by JC on 14th November, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment