Mathiew
Mathiew

Reputation: 61

Need help with a regex that needs to work with php

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!

Upvotes: 1

Views: 64

Answers (1)

Joe
Joe

Reputation: 15802

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.

Upvotes: 2

Related Questions