PHP Speed

Soldato
Joined
18 Apr 2004
Posts
2,613
Location
London
I wrote this last night to try and get an understanding of preg_replace and preg_match however it seems to run extremly slow to execute when there are more than 5 things to replace in a string so I was wondering if someone could point out where I am going wrong...

PHP:
// BBcode
		$pattern = array(
										'/\[b\](.+?)\[\/b\]/i',
										'/\[u\](.+?)\[\/u\]/i',
										'/\[i\](.+?)\[\/i\]/i',
										'/\[url\](.+?)\[\/url\]/i',
										'/\[url="(.+?)"\](.+?)\[\/url\]/i',
										'/\[url=(.+?)\](.+?)\[\/url\]/i',
										'/\[img=(.+?)\]/i',
										'/\[img="(.+?)"\]/i',
										'/\[img\](.+?)\[\/img\]/i',
										'/\[email\](.+?)\[\/email\]/i',
										'/\[email="(.+?)"\](.+?)\[\/email\]/i',
										'/\[email=(.+?)\](.+?)\[\/email\]/i',
										'/\[font="(.+?)"\](.+?)\[\/font\]/i',
										'/\[size="(.+?)"\](.+?)\[\/size\]/i',
										'/\[colour="(.+?)"\](.+?)\[\/colour\]/i',
										'/\[color="(.+?)"\](.+?)\[\/color\]/i',
										'/\[font=(.+?)\](.+?)\[\/font\]/i',
										'/\[size=(.+?)\](.+?)\[\/size\]/i',
										'/\[colour=(.+?)\](.+?)\[\/colour\]/i',
										'/\[color=(.+?)\](.+?)\[\/color\]/i',
										'/\[code\](.+?)\[\/code\]/i',
										'/\[quote\](.+?)\[\/quote\]/i',
										'/\[quote="(.+?)"\](.+?)\[\/quote\]/i',
										'/\[quote=(.+?)\](.+?)\[\/quote\]/i',
										'/\040([A-z0-9]+\:\/\/[A-z0-9]+\.+[A-z0-9_\-]+\.+[A-z0-9\.]{2,5}+[A-z0-9\.\?\=\/]+.)\040/i',
										'/\040([A-z0-9]+\.+[A-z0-9_\-]+\.+[A-z0-9\.]{2,5}+\/+[A-z0-9\.\?\=\/]+.)\040/i',
										'/\040([A-z0-9\.\_\-]+\@+[A-z0-9\.\_\-]+(\.[A-z]{2,4}|\.[A-z]{2,4}\.[A-z]{2,4}))/i',
										);

	$replace = array(
										'<b>$1</b>',
										'<u>$1</u>',
										'<i>$1</i>',
										'<a href="$1">$1</a>',
										'<a href="$1">$2</a>',
										'<a href="$1">$2</a>',
										'<img src="$1" />',
										'<img src="$1" />',
										'<img src="$1" />',
										'<a href="mailto:$1">$1</a>',
										'<a href="mailto:$1">$2</a>',
										'<a href="mailto:$1">$2</a>',
										'<font face="$1">$2</font>',
										'<font size="$1">$2</font>',
										'<font color="$1">$2</font>',
										'<font color="$1">$2</font>',
										'<font face="$1">$2</font>',
										'<font size="$1">$2</font>',
										'<font color="$1">$2</font>',
										'<font color="$1">$2</font>',
										'<pre>$1</pre>',
										'<blockquote>$1</blockquote>',
										'<blockquote><b>Quote from $1</b><br />$2</blockquote>',
										'<blockquote><b>Quote from $1</b><br />$2</blockquote>',
										' <a href="$1">$1</a> ',
										' <a href="http://$1">$1</a> ',
										' <a href="mailto:$1">$1</a> ',
	);

	if ($enable_bbcode) {$content = preg_replace($pattern, $replace, $content);};
 
Are you sure it's the preg_replace() that's causing the slowdown. Have you run a profiler (e.g XDebug/APD)?

Pattern replacements like that are very common, and I've not seen slow execution myself.
 
Last edited:
Augmented said:
Are you sure it's the preg_match() that's causing the slowdown. Have you run a profiler (e.g XDebug/APD)?

Pattern replacements like that are very common, and I've not seen slow execution myself.

I will try that, its just that without preg_replace its fine its slow with preg_replace, thanks for the link

EDIT: I cant run XDebug on Mac and APD needs a PECL add on which my webhost doesnt have
 
Last edited:
Not sure what to suggest really. Can you not install APD on your Mac? Pear/Pecl should be enabled with your PHP installation, then pecl install apd from a terminal (though I'm not familiar with PHP on Macs).

There's also Benchmark in Pear, but I've no idea how it fares.
 
IMO, if you want to replace a bunch of stuff with a bunch of other stuff, fast, then a custom parser is going to be miles quicker. I had to do a similar thing where I was originally using a bunch of regexps (c# .net, but the idea's probably similar). Switching to a parser where you step through your input string one char at a time for me was around 100-400 times faster. The more replacements it had to do, the faster it was.

Just a thought.
 
Back
Top Bottom