note 34097 added to function.str-replace

From: Date: Wed, 16 Jul 2003 10:13:17 +0000
Subject: note 34097 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-52185@lists.php.net to get a copy of this message
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; ?> ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34097 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34097&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34097&report=yes

« previous php.notes (#52185) next »