note 52284 added to function.ereg

From: Date: Wed, 27 Apr 2005 02:00:53 +0000
Subject: note 52284 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-88602@lists.php.net to get a copy of this message
After a lot of hard work I managed to create the following regular expression, which matches any HTML tag pair (i.e. opening and closing tag), as specified by tagname: ^(.*)(<[ \n\r\t]*tagname(>|[^>]*>))(.*)(<[ \n\r\t]*/[ \n\r\t]*tagname(>|[^>]*>))(.*)$ The expression is deliberately very forgiving of bad HTML - I wanted to match anything that could be reasonably accepted by a forgiving browser, rather than make it standards compliant. Whitespace is allowed between the tagname and the opening and closing tag symbols, and also between the / and the tagname for the closing tag. For my own use, I have wrapped it in a function call, which you may find useful. Here it is with a few notes. I hope somebody finds it useful. - Mark Clements <?php function ereg_MatchedHTMLTags($tagname) { return "^(.*)(<[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)(<[ \\n\\r\\t]*/[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)$"; } // Use with eregi to ensure case-insensitive match. // e.g. to split an HTML page based on body tag: // eregi(ereg_MatchedHTMLTags('body'), $Source, $Matches) // The following values will be held in $Matches //(marked values are unintended byproducts of the expression) // *[0] - the entire string ($Source). // [1] - everything before the opening tag // [2] - the opening tag, including all contents (i.e. everything between < and >) // *[3] - the opening tag from end of the tag name, // e.g. '<body bgcolor="#000000">' gives ' bgcolor="#000000">' // [4] - the tag contents (everything between the opening and closing tag) // [5] - the complete closing tag. // *[6] - the closing tag from the end of the tag name // e.g. '</body invalid text>' gives ' invalid text>' // [7] - everything after the closing tag. ?> ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52284 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52284&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52284&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52284&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52284&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52284&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52284&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88602) next »