Topic: Query regarding the preparse_bbcode() function.
I was having a browse of the parser implementation for inspiration, and wondered what the purpose of lowercasing the simple bbcode tags in preparse_bbcode() (from includes/parser.php) was?
Namely the following snippet:
// Change all simple BBCodes to lower case
$a = array('[b]', '[i]', '[u]', '[/b]', '[/i]', '[/u]');
$b = array('[b]', '[i]', '[u]', '[/b]', '[/i]', '[/u]');
$text = str_replace($a, $b, $text);
I understand fully what it's doing there - that wasn't what piqued my curiosity. It was over the decision to do that first and then run a case-sensitive regexp in do_bbcode(), rather than just using a single case-insensitive regexp in do_bbcode() instead. I ran a brief profile (PHP5) for comparison and running str_replace() first nearly doubles the processing time. 20s vs 35s for 100,000 iterations, comparing:
$pattern = array('|\[b\](.*?)\[/b\]|is');
$replace = array('<strong>$1</strong>');
$the_string = preg_replace($pattern, $replace, $the_string);
against:
$upper = array('[b]', '[/b]');
$lower = array('[b]', '[/b]');
$the_string = str_replace($upper, $lower, $the_string);
$pattern = array('|\[b\](.*?)\[/b\]|s');
$replace = array('<strong>$1</strong>');
$the_string = preg_replace($pattern, $replace, $the_string);
Please bear in mind I'm not trying to assert a 'right' or 'wrong' way, just curious about the reasoning, and whether it could be considered to have any real impact on performance?