Jump to content

Deleting Rows on Text File


mcmuney

Recommended Posts

I have a function where it writes data on a text file with an assigned id. For example, the text file looks like:

 

data.txt

 

1|Hey

45|Hi

76|Hello

 

What I'd like to do is add a function that will allow me to use the specific line that start with the id. For example:

 

delete.php : On this file I will pass the id number and based on it, the function will go to the data.txt file and delete the specific row. So if I pass the id=45, it will remove that row ONLY and the data.txt file will look like below after removal:

 

1|Hey

76|Hello

 

Any help will be greatly appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/64017-deleting-rows-on-text-file/
Share on other sites

This may help u,

1)Open a temporary file in write mode.

2)Read each line of the file using fgets.

3)'explode' each line using '|' as delimiter

4)Only if the first token is not equal to 45,write that line to temporary file

5)Once all the lines have been checked,unlink the original file & rename the temporary file to name of the original file

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.