Function for global replacement of string
| From: | mliceaga at softhome dot net | Date: | Fri, 29 Sep 2000 15:44:14 +0000 |
| Subject: | Function for global replacement of string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18065@lists.php.net to get a copy of this message | ||
Hi. I'm trying to do a function that replaces a string in all the files of
a site. The recursive part of reading the directories and entries works
fine.
The problem is with the replacement. Test it with a start point that does
not contain nothing important since all files will be truncated to zero.
I suppose that the problem is in the fread line, if I print $contenido it
outputs nothing, so when I use fwrite it writes nothing...
I think that if we can make it work it would be usefull for everybody..who
does not need to make a global replacement once in a while?
Thanx in advance!
<?php
function arbol_dir($rutadir){
@$dir = opendir($rutadir);
if(!$dir){ return 0; }
print "<ul>";
while($entry = readdir($dir))
{
if(is_dir("$rutadir/$entry") && ($entry != ".." &&
$entry != "."
))//IF IT'S A DIR AND NOT IS (.) OR (..) => IT'S A DIR
{
print "<li>$entry";
arbol_dir("$rutadir/$entry");
}
elseif(!is_dir("$rutadir/$entry") && ($entry != ".."
&&
$entry != "." ) && $entry!="reemplaza.php")//IF IT'S A FILE
{
if ((ereg(".php$", $rutadir."/".$entry) || ereg(".htm$",
$rutadir."/".$entry) || ereg(".html$", $rutadir."/".$entry) ||
ereg(".php3$", $rutadir."/".$entry) || ereg(".shtml$",
$rutadir."/".$entry)
|| ereg(".txt$", $rutadir."/".$entry)) &&
!(ereg("imagenes",$rutadir) ||
ereg("images",$rutadir) || ereg("phpMyAdmin",$rutadir)))//IF THE FILE IT'S
.PHP .HTM .PHP3 .HTML .TXT OR .SHTML AND IT'S NOT A DIR THAT I'M NOT
INTERESTED IN
{
$contenido="";
$archi=fopen($rutadir."/".$entry,"r");
set_magic_quotes_runtime(0);
//I GUESS THIS IS THE PROBLEM
$contenido = fread($archi,filesize($archi));
set_magic_quotes_runtime(get_magic_quotes_gpc());
fclose($archi);
$pe="old text";
$npe="new text";
$contenido=eregi_replace($pe, $npe, $contenido);
echo "<br>\n".$entry;
$archi=fopen($rutadir."/".$entry,"w");
fwrite($archi,$contenido,strlen($contenido));
fclose($archi);
}
}
else {}
}
print "</ul>";
}
//Start point
$ruta="/home/httpd/html/trash_dir";
$rutadir=$ruta;
arbol_dir($rutadir);
?>