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? smile

Re: Query regarding the preparse_bbcode() function.

Yes, but the str_replace() to convert the bbcode to lowercase isn't executed when displaying the post, only when inserting it into the db.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Query regarding the preparse_bbcode() function.

Aha, that's an important point I missed! As you probably gathered, I thought it ran every time any BBCode was processed. I see the benefit of doing that now, thanks for replying.