Page 1 of 1

Limited str_replace

Posted: Wed Dec 20, 2006 3:08 pm
by GeertDD
A quick question.

The standard str_replace function looks like this:
mixed str_replace ( mixed needle, mixed str, mixed haystack )

I'd like to turn it into this (in analogy with functions like explode and preg_replace):
mixed str_replace ( mixed needle, mixed str, mixed haystack [, int limit] )

Can't find a native php function that does this. Guess I'll have to create it myself or am I overlooking something?

Posted: Wed Dec 20, 2006 8:45 pm
by Ollie Saunders
Yeah I was annoyed by exactly the same thing the other day. Using preg_replace or preg_replace_callback was my solution.

Posted: Thu Dec 21, 2006 8:28 am
by GeertDD
Alright, but then you lose the speed of str_replace, unfortunately.

Posted: Thu Dec 21, 2006 9:43 am
by bokehman
GeertDD wrote:you lose the speed of str_replace
Is that a joke?

I find 1.4 microseconds difference (P4 3.2Ghz, 1024 RAM) between the two with the code below.

Code: Select all

<?php 

microtime(true);

$str = 'the quick brown fox jumps over the lazy dog';

$start = microtime(true);
for($i = 0; $i<1000000; $i++)
str_replace('brown', 'black', $str);
echo round(microtime(true) - $start, 1).' microseconds<br>';

$start = microtime(true);
for($i = 0; $i<1000000; $i++)
preg_replace('/brown/', 'black', $str);
echo round(microtime(true) - $start, 1).' microseconds<br>';

?>

Posted: Thu Dec 21, 2006 10:29 am
by Ollie Saunders
bokehman, you'll probably have to preg_quote though as well (make sure you specify the second argument).
GeertDD: worry about any performance problems you have when you have them rather than before. Unless a) you know you will have problems b) you can think of a better way quickly c) you're writing a library or similar. Consider CPU time over programmer time which costs more? :)

Posted: Thu Dec 21, 2006 11:26 am
by GeertDD
You're right, both. What am I worrying about? What's worse is that I was even trying to develop my own str_replace_limit function (without the use of preg_functions), just because I thought PCRE was slow.

Well, after benchmarking, my own function seemed to take twice the time of the preg_replace equivalent. Twice! And then the function does not accept arrays yet.

Code: Select all

// Beware, bad function, very slow!
function str_replace_limit($needle, $str, $haystack, $limit = 0) {
	
	$limit = max(0, (int) $limit + 1);
	
	if ($limit > 0)
		$arr = explode($needle, $haystack, $limit);
	else
		$arr = explode($needle, $haystack);
	
	$str = implode($str, $arr);
	
	return $str;
}
Thanks for the advice guys.

Posted: Thu Dec 21, 2006 12:31 pm
by GeertDD
Just noted that the time differences become a lot bigger when using a much longer string!

Preg_replace becomes a lot slower: 32.6 microseconds
While my own str_replace_limit is the fastest then: 3.39 microseconds

Hmm...

Posted: Fri Dec 22, 2006 3:09 am
by bokehman
GeertDD wrote:32.6 microseconds
Does that seem excessive to you. Put into perspective: If you were in a car travelling at 100 mph, it would be the amount of time it takes to move forward 1.43 millimetres (56/1000 inch).