Jump to content

Need help with a parser (Smileys + bbcode)


Nolongerused3921

Recommended Posts

Whats a good general runtime for a parser that gets smileys (str_replace) and bbcode (regex) from a database, then parses data input into it... Say a 200 character string.

Also, is there anything I can do to speed this up?
[code=php:0]
$sql = "SELECT * FROM `". $this->bbcode_db ."`";
$db->query($sql,'bbcode_query');
$pattern = array();
$replacement = array();
while ($bbcode = $db->fetch_row('bbcode_query')) {
        array_push($pattern,$bbcode[pattern]);
array_push($replacement,$bbcode[replacement]);
        }
$data = preg_replace($pattern, $replacement, $data);
$sql = "SELECT * FROM `". $this->smiley_db ."`";
$db->query($sql,'smiley_query');
while ($smiley = $db->fetch_row('smiley_query')) {
        $data = str_replace($smiley[pattern], "<img src='".$this->rdir.$smiley[replacement]."' />", $data);
        }
[/code]

The code is pretty self explaintory. (My mysql functions do what you'd expect in those cases, and $rdir is just the real directory since I'm using mod_rewrite)
Right now I'm sitting at 0.01 seconds to do various site things (About 0.0038 seconds) so the parser itself, to parse the string
[code]
WOOOOOOOOooo[b]ooooo[/b]Oooooo[i]oooo[/i]TAH! :)
[/code]

With 7 entries in the bbcode table, and 1 in the smileys table, takes around 0.0062 seconds to parse that string... Which I, personally, feel is pretty slow. Theres got to be a way to speed this up while leaving it open enough to add new smileys/bbcode easily in the future.
Link to comment
https://forums.phpfreaks.com/topic/32189-need-help-with-a-parser-smileys-bbcode/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.