Deleting text from a txt file

Hi

I am very very very new to php and just learning so please forgive the “simplicity” of this question.

I have this code:

$name= $_POST [‘name’];
$data= “data.txt”;

$filehandler = fopen($data, “a+”);
fwrite($filehandler, “$name \n” );
fclose($filehandler);

which writes to a text file

$name is an email address

Now I want to be able to create a form that will delete an email address from the txt file.

Could someone please help.

Thanks

Be something like this

[php]

<?php // if nothing has been posted print to screen if(!isset($_POST['submit'])){ ?> FIND
Replace
<?php } ?> <?php//if form has been posted && and filled out if(isset($_POST['submit']) &&(isset($_POST['findmail']))&&(isset($_POST['newmail']))){

// post the data to be replaced
$old = $_POST[‘findmail’];
$new = $_POST[‘newmail’] ;

// Get a file to change
$data = ‘data.txt’;
if(file_exists($data)){

// change the content with the posted content
$content = file_get_contents($data);
file_put_contents($data, str_replace($old, $new, $content));
echo ‘file has been modified’;
}else{ echo ‘no file to wirte to’;
}
}
?>[/php]

Tested version that works.

[php]

<?php // if nothing has been posted print to screen if(!isset($_POST['submit'])&&(!isset($_POST['findmail']))&&(!isset($_POST['newmail']))){ ?> FIND
Replace
<?php } ?> <?php //if form has been posted && and filled out if(isset($_POST['submit'])&&(isset($_POST['findmail']))&&(isset($_POST['newmail']))){ // post the data to be replaced $old = $_POST['findmail']; $new = $_POST['newmail']; // Get a file to change $data = 'data.txt'; if(file_exists($data)){ // change the content with the posted content $content = file_get_contents($data); file_put_contents($data, str_replace($old, $new, $content)); echo 'file has been modified'; }else{ echo 'Error no file to wirte to';} }else{ echo 'Error POST not set';} ?>

[/php]

OK thank you I will try.

Thank you for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service