note 34097 deleted from function.str-replace by didou

From: Date: Sat, 16 Aug 2003 09:51:26 +0000
Subject: note 34097 deleted from function.str-replace by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54313@lists.php.net to get a copy of this message
Note Submitter: nobody@hackingbord.de ---- on 25-Feb-2003 12:31, igjav at cesga dot es wrote:"So am I wrong if I say that str_replace doesn't makes sense,..." yes, you are. the difference between both functions ist the speed of execution. if you want to replace only single characters, it is faster to use strtr(). but if you want to replace bigger parts of a string, you'd better use str_replace. it is much, much faster in this case. try the following script to see what i mean. <?php $starttime = microtime(); $text = "abcdefg xyz aeoeue abcdefg xyz aeoeue abcdefg xyz aeoeue abcdefg xyz aeoeue"; $array = array("f" => "fff", "a" => "aaa", "e" => "eee"); $text2 = strtr($text,$array); $endtime = microtime(); $starttime = explode(' ', $starttime); $endtime = explode(' ', $endtime); $time = round(($endtime[0] + $endtime[1]) - ($starttime[0] + $starttime[1]),6); echo $time,"<br >"; echo $text2,"<br >"; $starttime = microtime(); $text = "abcdefg äöü aeoeue abcdefg äöü aeoeue abcdefg äöü aeoeue abcdefg äöü aeoeue "; $array1 = array("f", "a", "e"); $array2 = array("fff", "aaa", "eee"); $text2 = str_replace($array1,$array2,$text); $endtime = microtime(); $starttime = explode(' ', $starttime); $endtime = explode(' ', $endtime); $time = round(($endtime[0] + $endtime[1]) - ($starttime[0] + $starttime[1]),6); echo $time,"<br >"; echo $text2; ?>

« previous php.notes (#54313) next »