Author Topic: Mass search and replace with php.  (Read 543 times)

Offline toxin

  • Level 21
  • *
  • Posts: 231
  • Reputation: +4/-2
    • View Profile
    • Encore Montreal
Mass search and replace with php.
« on: September 16, 2009, 01:34:41 AM »
A script like this may have already been done but I thought to give it a shot. It may have a few bugs but it did work to a point. The only thing that i noticed is that it does not edit but copies and moves the files to the directory of the script. All it is search and replace files in a directory. Noting big but may be of some use. 
A note if you do use this it is advised that you back up the directory you are going to edit.
Code: [Select]
<?php
$dirtoedit
="edit";
$dir=dir($dirtoedit);
$i=1;
while ((
$file $dir->read()) !== false)
{
if (
$file!='.' && $file!='..' && $file!='.htaccess')
{
$search="echo";
$page=file_get_contents("$dirtoedit/$file");
if(
strstr($page,$search)){
$new=str_replace($search,"print",$page);
$f=fopen($file,"w");
//chmod may not be needed not sure
chmod($file,0644);
fwrite($f,$new);
fclose($f);
echo
"<h3>$i$file edited</h3>";
$i++;
}
}
}
$dir->close();
?>


« Last Edit: September 16, 2009, 01:38:22 AM by toxin »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal