Recursive eregi() function too slow

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
apg88
Forum Newbie
Posts: 16
Joined: Thu Aug 25, 2005 5:43 pm

Recursive eregi() function too slow

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_* is far faster than ereg_*

..and as we've seen, some stack based parsers are faster still :)
apg88
Forum Newbie
Posts: 16
Joined: Thu Aug 25, 2005 5:43 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

function parseCode($input)
{
        $input = preg_replace("#\[code\](.*?)\[/code\]#si","<div style=\"color: blue\">\\1</div>",$input);
        return $input;
    }
:)
apg88
Forum Newbie
Posts: 16
Joined: Thu Aug 25, 2005 5:43 pm

Post by apg88 »

Wow, now it is instantaneous!
Thank you very much!
Post Reply