1

I need help with this regex, I have to use it with PHP but I keep getting errors while using it with preg_match. I know I am doing something wrong but I can't figure it out. This regex finds/matches html tags in a string. If you have any other regex that can do this, please let me know!

</?(a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dir|div|dfn|dl|dt|em|fieldset|font|form|frame|frameset|h[1-6]|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var|xmp)\b((\"[^\"]*\"|\'[^\']*\')*|[^\"\'>])*>

        if (!preg_match("/</?(a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dir|div|dfn|dl|dt|em|fieldset|font|form|frame|frameset|h[1-6]|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var|xmp)\b((\"[^\"]*\"|\'[^\']*\')*|[^\"\'>])*>/", $input) && preg_match("/^.{2,$max_width}$/i", $input)) {
            $result = true;
        }

Thank you!

Mathiew
  • 61
  • 5
  • 2
    Please see http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags for more information. – Tesserex Sep 15 '11 at 22:14
  • What is this regex supposed to do? – Pekka Sep 15 '11 at 22:15
  • 1
    Welcome to StackOverflow. When asking a question, saying "I keep getting errors" without showing the **exact** error message you're getting is meaningless. So is saying "I need help with this" without explaining what "this" is supposed to do (or supposed to be); "finds/matches html tags in a string" doesn't say much. Please edit your post to make it an actual answerable question, or it will most likely be closed. When doing so, remember that we have no idea of what you're trying to do (or anything else) except what you put in your question. Thanks. :) – Ken White Sep 15 '11 at 22:16
  • 1
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Sep 15 '11 at 22:26

1 Answers1

2

Your regex starts with /</?(a|abbr|ac... - the delimiter character (denotes start and end of regex) is a forward slash. When it sees the second forward slash after < it thinks the regex is finished.

Change it to /<\/?(a|abbr|ac (escape the slash with a backslash) and it will work.

Joe
  • 15,669
  • 4
  • 48
  • 83