Fixed Redundant preg_match calls in XenForo_BbCode_Parser


Well-known member
In XenForo_BbCode_Parser::_parseTagOpen there is:

if (strlen($tagName) == 0 || preg_match('/[^a-z0-9_-]/i', $tagName) || !$this->_getTagRule($tagName))
    return false;
I'm fairly sure that the call preg_match isn't required as _getTagRule will do a fast isset lookup into the taglist, This tag list has already been verified that the tag names match that pattern anyway.

In XenForo_BbCode_Parser::_parseTagClose:

if (strlen($tagName) == 0 || preg_match('/[^a-z0-9_-]/i', $tagName))
If this isn't a valid tag, then it can't be considered to be a closing tag. So that preg_match could be replaced with a call to _getTagRule.

IMO the length check could be pushed into _getTagRule (and adding a check that the tag name isn't longer than 25 character) before the strtolower call is even done.
Last edited:


XenForo developer
Staff member
I've changed this now, though I don't think there's really any effective difference (likely close to negligible performance differences I think, assuming tag names are short).

I would hesitate to put an explicit length limit in right now, as there may be situations where longer tags have been used (though I hope not).
Reactions: Xon