Jump to content

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

 

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.