Jump to content

Delete Line from a text file with PHP


Daithi

Recommended Posts

Hi,

 

I am new to the php world & my head is wrecked.  I am trying to delete a line from a text file that contains mobile phone numbers. 

 

The mobile number to be deleted is passed onto the page from a dynamic form.  I want to then take this mobile number search the text file for it & then delete it from the text file.

 

Anything i have searched deletes the line number....

 

The text file is a list of mobile numbers 1 per line & no spaces.

 

Please Help!

Link to comment
Share on other sites

You probably get more help if you...

 

Post your current code

Post a sample of the text file

 

Also know, you can't just delete a line, you will need to read in the entire file, remove the parts that you don't want with PHP then re-write the new file.

Link to comment
Share on other sites

thanks well even the logic behind it that you explained there helps, as i said i am new.  from my reading up on it you have to

 

- open the text file

- copy contents of the text file to a new file except the line to be deleted

- delete the initial file & rename the temporary file to what the initial one was

 

??

 

the text file is just... numbers.txt formatted like this. 1 number on 1 line.  I am using one and just want to delete it from the list.

 

0861234567

0861234568

0861234569

0861234561

 

any more help, or even the correct logic how it happens & i can read up on the syntax, would be appreciated. :)

 

 

Thank You.

Link to comment
Share on other sites

1. open the text file using fopen in w+ mode

 

2.create a variable to temporarily storage of the line you are not looking for

 

3. read one line using fgets

 

4. compare the line contents with the data to be removed

 

5. if it is the one to be deleted skip that line, if its NOT add the line read to the temporary storage.

 

6. if its still not the end of file back to to set 3. use feof()

 

7. remove all the contents of the file using ftruncate()

 

8. write all the contents of the temp storage to the file

 

Hope that helps

Link to comment
Share on other sites

Got that going this morning.... thanks again here it is it might help someone like me! :)

 

 

 

 

 

<?

 

$search = $_POST['used'];

echo "The number $search has been used.<br><br>";

 

 

 

$file = "actn.txt";

$f = fopen($file, "r+");

 

 

 

$ammount = count(file($file));  // count lines in file

$ammount--;

 

 

 

 

for ($i=0; $i<=$ammount && $line = fgets($f, 1000); $i++) //place lines in an array except number that was used

{

 

if ($line != $search)

$ctns[$i] = $line;

 

}

 

 

 

 

ftruncate($f,0);  //delete file contents

$ammount--;     

 

 

 

 

for ($i=0; $i<=$ammount; $i++)  // write array to text file

{

 

$stringData = $ctns[$i];

fwrite($f, $stringData);

 

 

}

 

 

fclose($f);

 

 

 

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.