Click to See Complete Forum and Search --> : Delete all occurance
ashishrathore
03-04-2006, 09:10 AM
hi,
i want to delete all occcurances of a word from a file (a text file)
how can i do that???
for eg..
=======================TEXT FILE===========================
ashish@yahoo.com
manoj@yahoo.com
avinash@lala.com
devi@qwe.com
yahoo@yahoo.com
gmail@gmail.com
ramesh@ramesh.com
raj@raj.com
....
.
.
..........
.
.
=========================================================
here i want to delete one particular email id
how can i do that?
any ideas???
-ashish
bokeh
03-04-2006, 09:32 AM
Grab the file contents with file_get_contents and then uses str_replace to remove the unwanted string.
ashishrathore
03-04-2006, 09:40 AM
thanks bokeh
LiLcRaZyFuZzY
03-04-2006, 09:55 AM
or you can use this, email.txt being the file containing your e-mail addresses
<?php
$file = "email.txt";
$email = "avinash@lala.com";
function del_email_address($email, $file){
$lines = file($file);
foreach($lines as $key => $address){
$address = trim($address);
if($address == $email){
unset($lines[$key]);
}
}
$file_content = implode("", $lines);
$fp = @fopen($file, "w");
$write = fwrite($fp, $file_content);
if($write != 0){
echo "Success";
}else{
echo "Could not write to file";
}
fclose($fp);
}
del_email_address($email, $file);
?>
ashishrathore
03-04-2006, 11:36 AM
hey thanks buddy
but i want some changes in that
suppose the entered Email-Id is not in the file then the code must exit from the loop and print some message such as "No Record Found" like that and if the record is found then it must delete as it was b4
-ashish
LiLcRaZyFuZzY
03-04-2006, 11:57 AM
<?php
$file = "email.txt";
$email = "avinash@lala.com";
function del_email_address($email, $file){
$lines = file($file);
foreach($lines as $key => $address){
$address = trim($address);
if($address == $email){
unset($lines[$key]);
$found = true;
}
}
if($found){
$file_content = implode("", $lines);
$fp = @fopen($file, "w");
$write = fwrite($fp, $file_content);
if($write != 0){
echo "Success";
}else{
echo "Could not write to file";
}
fclose($fp);
}else{
die("No Record Found.");
}
}
del_email_address($email, $file);
?>
SpectreReturns
03-04-2006, 05:58 PM
How about something like
$file = file("emails.txt");
$data = str_replace("string to delete", null, $file);
if ($data == $file) {
die("No record found.");
} else {
unlink("emails.txt");
file_put_contents("emails.txt", implode(null, $data));
}
Keep it simple.
bokeh
03-05-2006, 03:31 AM
How about something like
$file = file("emails.txt");
$data = str_replace("string to delete", null, $file);
if ($data == $file) {
die("No record found.");
} else {
unlink("emails.txt");
file_put_contents("emails.txt", implode(null, $data));
}
Keep it simple.Why use unlink. file_put_contents would overwrite the old file anyway... or am I missing something?
SpectreReturns
03-05-2006, 05:35 PM
I thought file_put_contents would append the data. I just checked, and you're right. Could be effectively changed to:
$file = file("emails.txt");
$data = str_replace("string to delete", null, $file);
if ($data == $file) {
die("No record found.");
} else {
file_put_contents("emails.txt", $data); // it'll already implode on null
}