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);};
 
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:
Back
Top Bottom