Results 1 to 12 of 12
  1. #1

    PHP question (I'm stumped)

    I've asked a few programmers this question and nobody seems to know the answer... I've finally resorted to asking at a message board (oh the shame! lol)

    Ok, let's say I have a list of things in an html file

    Item 1<br>
    Item 2<br>
    Item 3<br>
    Item 4<br>
    Item 5<br>
    What I want is a PHP script where I can enter an item name (say item 3) and have the script delete *only* that specific content from the file.

    So, how do I write a script that opens up the file, finds the specified text and deletes that text? Is it even possible?

  2. #2
    Join Date
    Mar 2004
    if its the same format as above:

    $f = fopen('filename','r');
    $text = fread($f,filesize('filename'));

    $break_it = explode("<br>",$text);

    foreach($break_it as $line){
    if($line != $item_toberemoved){
    $new_file .=$line.'<br>';

    $f2 = fopen('filename','w+');

    What we are actually doing is copying the file, removing the item we dont need and then making the file again..

    Testing 1.. Testing 1..2.. Testing 1..2..3...

  3. #3
    Hmmm... nice script, but I don't think it'll work. My fault, I should've mentioned that the file with the list is being updated a lot. Think of a list of users in a chat room (that's not what it is, but that example will do nicely). As people log in and out the list constantly changes. The list I want needs to do the same (only based on a variable being passed through a form, not based on sessions). Writing to the list to add items is easy, but I'm having a heck of a time figuring out how to remove the items from the list on command.

  4. #4
    Ok, a little more detail for you. Here's the relevant portion of the code:

    if ($mode == 'additem') {
    $list=fopen("list.php", "a");
    fwrite($list, "$item<br>");
    } elseif ($mode == 'delitem') {

  5. #5
    Join Date
    Dec 2004
    Make absolutely sure you wrap that code in the appropriate flock() calls if you're expecting concurrent access. You'll likely get data corruption (or more likely, you'll lose a submission) eventually if you don't.

    The basic algorithm behind doing something like this is:
    1) Read the entire file into memory
    2) Modify it in-memory to remove the part you don't want (how this would be done is up to you, it should be reasonably simple, but I hate doing text processing so I won't do it for you)
    3) Write the entire file out again

    I must ask though, while I don't think databases are the answer to everything, this seems to be a prime candidate for a database-based solution. Why aren't you using one? The text-based code will be more bug-prone and a lot more work.

  6. #6
    Join Date
    Jul 2003
    You can just do this (if you have access to sed):

    PHP Code:
    <?php exec("sed -e '/Item 3/d' < somefile.txt"); ?>
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  7. #7
    Join Date
    Jan 2003
    SQL would be a good choice for the workload your describing. Even if you stick to flat files, I suggest using a file format that is convenient for the add/remove activity, rather than trying to retrofit the code to handle the format. If you wish, you can still have the code generate a static HTML file, but you would need a LOT of traffic to notice a difference over generating it on the fly.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

  8. #8
    Thanks for the suggestions, guys.

    fyrestrtr, that would work wonderfully if I had access to SED...

    error404 and hiryuu, I agree that using a database would probably make this a whole lot easier. Unfortuneately, I am a newbie at PHP. Still learning the ropes and I have not yet learned how to deal with databases. I can always upgrade this script to work better once I understand DBs, but for now I just wanted to have something that would work.

    I'm guessing that since none of the suggestions thus far have worked, then there probably isn't any way to do this without using a DB. Oh well, it was worth a shot.

    A very big thank you to everyone who responded in this thread. I really appreciate your help.

  9. #9
    Join Date
    Jan 2005
    Er, didn't test my code enough. I'm looking at a solution for you, though. I'll post it if it actually works next time..heh.
    Last edited by intransit; 05-08-2005 at 12:09 AM.

  10. #10
    Join Date
    Jan 2005
    Ok, I tried to optimize it and ended up just screwing it up. But anyways, this should do what you need. As far as I know, the only way to edit a flat file is to write it back over itself. Even if you are doing this many times it wont be a concern as far as speed goes, unless the file is thousands of lines long, at which point a database would be a better solution. This is somewhat similar to what azizny wrote, but if you have any problems with it pm me.

    PHP Code:

    //test variables
    $item="Item 2";

    if (
    $mode == 'additem') {
    } elseif (
    $mode == 'delitem') {

    //load file into an array

    //remove <br>, trim string

    //delete desired item

    //open file for writing, truncated
    $list as $line){

    //if it's not the line we deleted, write it back into the file
    Good luck.

  11. #11
    Join Date
    Mar 2004
    New Zealand
    Don't forget to add locking to that script!

  12. #12
    Join Date
    Oct 2002
    Why not just create folder and have one file in it representing each line of current text file. Then you would just have to create and unlink files and wonld not have to worry about data corruption and locking too much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts