RE: [PHP] Deleting a string from a text file

From: Date: Wed, 04 Jul 2001 06:14:35 +0000
Subject: RE: [PHP] Deleting a string from a text file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-56109@lists.php.net to get a copy of this message
you could also do it this way: $ofile = "file.txt"; // original file $nfile = "new.file.txt"; // new file $string = "original"; // what needs to be replaced $replace = "replacement"; // what to replace it with $data = file($ofile); // original file's data $fp = fopen($nfile,"w") or die("Cannot open for writing"); while ( list(,$line) = each($data) ) { $line = preg_replace("/$string/", $replace, $line); fwrite($fp, $line) or die("Cannot write"); } fclose($fp); $r = copy($nfile,$ofile) or die("Unable to copy"); $r = unlink($nfile) or die("Cannot unlink");

« previous php.general (#56109) next »