Page 1 of 1

Recursive eregi() function too slow

Posted: Thu Aug 25, 2005 5:58 pm
by apg88
I am creating my own little BBcode like script in PHP.

I'm just starting, but it seems way to slow to run.

Here is the function that replaces [ code ] This code will be colored blue[ /code ] into blue text without the [ code ] tags.

Code: Select all

function parseCode($input){

   if (eregi("(.*)(\

Code: Select all

)(.*)(\[/code\])(.*)",$input, $regs)) {
      //$regs[3] = nl2br($regs[3]);

      $input = $this->parseCode("$regs[1] <div style=\"color: blue\"> $regs[3] </div> </b> $regs[5]");
   } else {
      return $input;
   }

   return $input;
}
Does anyone know of a faster, more effective way to do this?

Thank you,

apg88



feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Aug 25, 2005 6:04 pm
by feyd
preg_* is far faster than ereg_*

..and as we've seen, some stack based parsers are faster still :)

Posted: Thu Aug 25, 2005 6:27 pm
by apg88
Thanks, the only problem now, is that it doesnt work :-(

Here is the new funciton:

Code: Select all

function parseCode($input)
	{
		$input = preg_replace("(.*)(\

Code: Select all

)(.*)(\[/code\])(.*)","$1 <div style=\"color: blue\"> $3 </div> $5",$input);
		return $input;
	}
Any ideas?

Posted: Thu Aug 25, 2005 6:34 pm
by feyd

Code: Select all

function parseCode($input)
{
        $input = preg_replace("#\[code\](.*?)\[/code\]#si","<div style=\"color: blue\">\\1</div>",$input);
        return $input;
    }
:)

Posted: Thu Aug 25, 2005 6:49 pm
by apg88
Wow, now it is instantaneous!
Thank you very much!