PDA

View Full Version : Please help me with deleting one line from a .txt file


Penne
08-23-2002, 11:52 AM
i have a flatfile database for storing email addresses (actulaly im trying to make a mailing list). i already made it possible to store an email address. the database is in format:

1@pennes.net
2@pennes.net
3@pennes.net
... and so on...



now here is what i was thinking of:

$fd = fopen($dbfile_path, "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);

if ($buffer==$email){
// ERASE LINE CODE - but how??
echo "email $email has been successfuly deleted";
}
}
fclose ($fd);

i was thinking about if i would check for line length, than i would delete that many characters. anyway, whoever tells me this i will make a favor for that person to thank him/her. - at least give me an example or something i can work with.

thank you,

Strike
08-23-2002, 12:29 PM
um, what are the criteria for what line you want to delete? If you want to just do it once .. just do it in a text editor.

Grizzly
08-23-2002, 01:54 PM
This is one of the basics of data storage and retrieval. In your case, you want to delete a record from a sequential file. ANYTIME you want to delete a record from a sequential file, you have to read the entire file into memory, remove the desired record, and re-write the entire file back out to disk.

I wrote a text-file based message board system a while back (www.bazookaboard.com), which provides the ability for Administrators to delete messages from the text file, and basically follows the same principle mentioned above. The only real difference between the bazookaboard data, and your data, is the fact that one "record" consists of 6 lines, not 1. With that in mind, take a look at this code snippet from the bazookaboard, found in 'act_DeleteMessage.php':


$j=0;
$NewFileContent = array();
$fp = fopen($DataFile, "r+");
$FileContent = fread($fp, filesize($DataFile));
fclose($fp);

$FileContent = explode($NewLine, $FileContent);

for ( $i=0; $i < sizeof($FileContent); $i+=6 ){
if( strlen(trim($FileContent[$i]))){
if( !((intval($FileContent[$i+1]) == intval($TopicID)) && (intval($FileContent[$i]) == intval($TimeStamp))) ){
$NewFileContent[$j] = $FileContent[$i];
$NewFileContent[$j+1] = $FileContent[$i+1];
$NewFileContent[$j+2] = $FileContent[$i+2];
$NewFileContent[$j+3] = $FileContent[$i+3];
$NewFileContent[$j+4] = $FileContent[$i+4];
$NewFileContent[$j+5] = $FileContent[$i+5];
$j+=6;
}
}
}

$NewFileContent = implode($NewLine,$NewFileContent);
if(strlen($NewFileContent)>1) $NewFileContent .= $NewLine;
$fp = fopen($DataFile, "w+");
fwrite($fp, $NewFileContent);
fclose($fp);



Looking at that you should get the gist of what's going on here, and you should be able to figure out what you need to do to delete one "record" from your text file.

Good luck!:P

Penne
08-24-2002, 04:11 PM
strike: the criteria is just - delete the line that contains the specified email address - remove that one from the list, and than move all the below ones up for one line.
grizli: thanks for reply and example but i just dont get it.

ok well... i must be quite dumb coz i dont understand this.


$i=0;
$file = array();
$newfile = array();
$fp = fopen($dbfile_path, "r+");
$emails = file($elist_path);
fclose($fp);

do {
$i += 1;
echo "email: $emails[$i] not erased"; //found but doesnt match-not erased
} while($emails[$i] == $email);

echo "found: $emails[$i]";



this is what i have been trying to do, but it returns:
email: not erasedfound:

so i guess $emails[] isnt defined or what? can you please please help me because i would like to learn.

roninblade
08-28-2002, 10:59 PM
try this. haven't tested it though. ;)
<?php
$emails = file($db_file_path); // reads entire file into an array

$key = array_search($email_to_delete, $emails);
if (empty($key)) {
echo 'email not found
';
} else {
unset($emails[$key]); // removes the email address from the array
$new_content = implode("\\n", $emails);
$f_id = fopen($db_file_path, "w");

if (!fputs($f_id, $new_content)) echo 'error writing emails
';
else echo 'email has been succesfully deleted';

fclose($f_id);
}
?>