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

2001-07-03 Thread ..s.c.o.t.t..

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");



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




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

2001-07-03 Thread David Robley

On Tue,  3 Jul 2001 18:04, Stevenson, Christopher wrote:
> Hello, folks. I'm relatively new to this.
>
> Would anyone be willing to give me some code that will delete the first
> occurrence of a string from a text file?
>
> Thanks,
> Chris.

Hello Chris - when did you guys discover PHP!

Grab the file into an array with file(), loop through the array and do a 
ereg or substr to find the string, str_replace to replace the string 
(with a counter to ensure you only do the first occurrence) then fopen 
the file, reset the array  to the first element and loop throug the array 
writing each element back to the file. Code might look like this:

$string_to_find = 'whatever';
$file = 'filename.txt';  //assuming its in CWD
$data = file($file);  //$data is an array with the file contents
$changed = 0   // flag to indicate you've found a string to change
while (list($key , $curr_line) = each($data)) {
  if(changed != 0) {
   break;  //Already got first replacement - finish
  }
  if (substr($string_to_find, 0)) {
str_replace($string_to_find, '', $curr_line);
$changed = 1;
  }
}
//Now write data back to the file
$file_pointer = fopen($file,"w");  //Open file for writing
reset($data);  //Set array pointer to first element
/ And loop throgh array writing each element (line) back to file
while (list($key , $curr_line) = each($data)) {
  fwrite($file_pointer, $curr_line);
}
// Close the file nicely
fclose(fpointer);

That's just one way of doing it - I'm sure there will be other 
suggestions. And of course it depends on what's in the string you are 
trying to delete; you may have to ereg and ereg_replace.

This code is untested, of course - the usual disclaimers apply.

Cheers
-- 
David Robley  Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES  Flinders University, SOUTH AUSTRALIA  

   The first step to making a dream come true is to wake up

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]