View Single Post
  #50 (permalink)  
Old 11-08-2007, 04:47 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,
Optimizing the str_replace()
The str_replace() function in PHP can be slow, due it’s duplication of data even if no replacement is being performed.

PHP Code:
$src_str file_get_contents("CONTENT_FILE_PATH");
$src = array('abc'123'text');
$dst = array('cba'321'txet');
// eliminate unnecessary replacement attempts
foreach ($src as $k => $v)
if (
strpos($src_str$src) === FALSE)
unset(
$src[$k], $dst[$k]);
if (
$src$new_str str_replace($src$dst$src_str); 
Click to view the difference of strtr() and str_replace()

Thanks,
R.Gopi
Reply With Quote