#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    UK
    Posts
    17
    Rep Power
    0

    Question Simple find and replace word search for a text file with php?


    hi,

    All I need to know is how to search a text file called loginA.txt for a word that is equal to $nickname
    When it finds the word I want it to remove the word from the text file. Then write the new data back the file.

    So if the $nickname was “dog” and the list in the text file was “me,fun,dog,mouse,house,”
    Then the php would remove “dog” and save the text file like this “me,fun,mouse,house,”

    All the text must be on one line only

    The way the words in the text file are stored is like this: “me,fun,dog,mouse,house,”

    Each word is separated by a comma like ","

    I will have to remove the comma too so than in fact I will be removing the word and the one comma that follows after
    e.g. "dog,"
    that’s so I do not get “me,fun,,mouse,house,” with two commas next to each other. Has to be “me,fun,mouse,house,”

    I can not use MYSQL and have to use a text file.

    How do I do this?

    Many thanks
    Last edited by rabid lemming; February 1st, 2004 at 04:57 PM.
  2. #2
  3. No Profile Picture
    Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,650
    Rep Power
    2479
    PHP Code:
    $words explode","implode""file"/path/to/loginA.txt" ) ) );
    foreach( 
    $words as $i => $w ) {
      if( 
    $w == $nickname ) {
        unset( 
    $words$i ] );
      }  
    }
    $fileHandle fopen"/path/to/loginA.txt""w" );
    fwrite$fileHandleimplode","$words ) );
    fclose$fileHandle ); 
    Last edited by php4geek; February 1st, 2004 at 07:23 PM.
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    UK
    Posts
    17
    Rep Power
    0
    hi,

    it dosnt seam to to be removing the word?

    any idea why?

    cheers
  6. #4
  7. No Profile Picture
    Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,650
    Rep Power
    2479
    I fixed it. It should work. (I tested it on my WAMP.)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    UK
    Posts
    17
    Rep Power
    0
    hi,

    I got it

    I used the following:

    <?
    $data = file_get_contents("loginA.txt");
    $data = explode(",", $data);
    foreach($data as $val) {
    if($nickname != $val) {
    $newdata .= $val . ",";
    print "$val<br />\n";
    }
    }
    $newdata = substr($newdata, 0, -1); // remove trailing ,
    print "<br /><br />String: $newdata";
    $fp = fopen("loginA.txt", "w+");
    $fw = fwrite($fp, $newdata);
    fclose($fp);

    ?>

    Worked like a charm,

    Thanks for your help though

IMN logo majestic logo threadwatch logo seochat tools logo