Re: Function for global replacement of string

From: Date: Fri, 29 Sep 2000 09:59:12 +0000
Subject: Re: Function for global replacement of string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-18066@lists.php.net to get a copy of this message
You could use grep to do this. Is the purpose to try something different and learn something, or is it to simply have a functional tool? ----- Original Message ----- From: <mliceaga@softhome.net> To: <php-general@lists.php.net> Sent: Friday, September 29, 2000 4:44 PM Subject: [PHP] Function for global replacement of string > > 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); > > > ?> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#18066) next »