Re: preg_match question: locating unmatched HTML tags
| From: | Tom Rogers | Date: | Sun, 23 Feb 2003 04:40:39 +0000 |
| Subject: | Re: preg_match question: locating unmatched HTML tags | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-136699@lists.php.net to get a copy of this message | ||
Hi,
Saturday, February 22, 2003, 12:35:15 PM, you wrote:
AC> My apologies in advance if this too basic or there's a solution easily
AC> found out there, but after lots of searching, I'm still lost.
AC> I'm trying to build a regexp that would parse user-supplied text and
AC> identify cases where HTML tags are left open or are not properly
AC> matched-e.g., <b> tags without closing </b> tags. This is for a sort of
AC> message board type of application, and I'd like to allow users to use
AC> some HTML, but just would like to check to ensure that no stray tags are
AC> input that would screw up the rest of the page's display. I'm new to
AC> regular expressions, and the one below is as far as I've gotten. If
AC> anyone has any suggestions, they'd be very much appreciated.
AC> Thanks,
AC> Andy
AC> $suspect_tags = "b|i|u|strong|em|font|a|ol|ul|blockquote ";
AC> $pattern = '/<(' . $suspect_tags . '[^>]*>)(.*)(?!<\/\1)/Ui';
AC> if (preg_match($pattern,$_POST['entry'],$matches)) {
AC> //do something to report the unclosed tags
AC> } else {
AC> echo 'Input looks fine. No unmatched tags.';
AC> }
The simplest is just to add </b></i></textarea></whatever else you allow>
directly to the end of their
message, may not be technically correct but it won't do any harm either :)
--
regards,
Tom