Limited str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Limited str_replace

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Alright, but then you lose the speed of str_replace, unfortunately.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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>';

?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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? :)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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).
Post Reply