Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing flat files. 3

Status
Not open for further replies.

cian

Technical User
Joined
Oct 11, 2001
Messages
1,383
I'm creating a small application which will use txt files as the database (or an xml file if that's easier)

I will append lines of HTML+text into this database from a HTML form but I need to be able to remove specific lines of text from the db.
For example, if I was creating a list of bookmarks, I have no problem adding to the list but how do I delete one item from the middle of the list?

I haven't yet come across a tutorial which shows how to do this. All I have found is that I would need to read, edit and rewrite the complete content of the file but where do I start with this, or is there a better way?
Can anyone advise me?




- É -
 
The only real way to remove a line from a text file of that type is to copy the entire file except for the line you don't want.
 
sounds like a horrible task.
I though of one way, to lead the contents into a text area and edit directly, but that will display HTML also.

Is there a way to sort the data into an array and delete that one item from the array? Just a thought.




- É -
 
You can open the file and use file() - which reads it in an array (exploding on the newlines).

There you could use the array manipulation functions to remove the desired line.

Keep in mind that the array of lines still has the newline at the end of the values.
 
Keep in mind, too, that if you are programming for the web, you should assume you are writing a multi-user application.

If one instantiation of your script is changing the file, you need to be sure that other instantiations of the script aren't trying to change it at the same time.

I recommend you take a look at flock() (
Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks to all,

sleipnir214, yeah i'm basically familiar with flock(), this is planned to be a single user script but I may include that anyway.

DRJ478, that looks like the direction I am heading. I was reading on that last night but had trouble finding out how to delete one item from an array. any specific functions I should use? (i'm a php beginner!)

Thanks again,





- É -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top