SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    editing one line in file

    i have a file called "test.txt"

    this is what it looks like

    6
    new
    old
    now
    then
    why
    what

    i want to change only the first line, that is change number 6 to any other number. How can i do this?

  2. #2
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't edit one line in a file, what you have to do:

    Open your file in php
    Put your file in a variable
    $variable = ereg_replace("6",$other_number,$variable);
    and then replace the whole file:
    PHP Code:
    $file "name_of_your_file.txt";
    $fe fopen($file,"w");
    $write fwrite($fe$variable);
    $write .= fclose($fe);
    if(!
    $write){
    echo
    "Error while writing to \"$file\"";
    }
    else{
    echo
    "Updated!";


  3. #3
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi iBram007,
    Thank you very much for the code, but the number 6 was just a sample of the number that will be there. I cant keep track of the number. So I cant do a ereg_replace.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This ought to serve your purpose, it basically reads the entire text file into an array and then loops through it replacing the first element with whatever you choose adn then builds a new array. Then it writes that new array back to the text file.

    PHP Code:
    <?
    $in 
    file("test.txt");
    $newnumber 10;
    $counter 0;
    foreach(
    $in as $key => $line) {
        if(
    $counter == 0) {
            
    $line $newnumber;
            }
        
    $out[] = chop($line);
        
    $counter++;
        }
    $outfile fopen("test.txt""w");
    foreach(
    $out as $key => $line) {
        
    fputs($outfile$line."\n");
        }
    fclose($outfile);
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, then how are you determining which part/line/number you want to edit? Need a bit more info here. Oh, and if you were going to replace 6, for example, I'd recommend str_replace(), seeing as how a regular expression would not be necessary. It's a tiny bit quicker.

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ereg_replace() and str_replace are no use here all he wants to do, at least from his first post is to replace the very first line of the text file. I think my method above will be the quickest way top achieve this. Although I would love for someone to prove me wrong here
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not at all -- I wasn't really attempting to solve the problem, just wanted to note that, if he were going to replace a specific number like that, as someone else mentioned, it'd be better to use that function. As for his post: heck, I'm stumped. He says the first line of the file, but I don't know if he means the first line EVERY time, or just in this example. I dunno.

  8. #8
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to replace the conent of the first line of the file only. Nothing else.

    I tried the code freddy gave me. I get this error
    Warning: Failed opening '/path/to/file/index.php' for inclusion (include_path='') in Unknown on line 0


    i have changed the path to the file when posted here. But the the path in the script is correct.

    Do i need to chmod any files?

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the code you tried?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    $file = "/home/boo/public_html/test.txt";
    $in*=*file($file);
    $newnumber*=*10;
    $counter*=*0;
    foreach($in*as*$key*=>*$line)*{
    ****if($counter*==*0)*{
    ********$line*=*$newnumber;
    ********}
    ****$out[]*=*chop($line);
    ****$counter++;
    ****}
    $outfile*=*fopen($file,*"w");
    foreach($out*as*$key*=>*$line)*{
    ****fputs($outfile,*$line."\n");
    ****}
    fclose($outfile);
    ?>

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should work, are you sure you have the path right? The file must be wriatble by the web server for this work. try chmod the file to 777 or 766.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    $file = "/home/unreg/public_html/traffic/site.txt";

    $t = file($file);
    $total_links = count($t);
    $to_visit = $t[0];
    $new_visit = $to_visit+1;
    if ($new_visit > $total_links) {
    $new_visit = 1;
    }

    $new_array[0] = "$new_visit";
    for ($i=0;$i<$total_links;$i++) {
    $new_array[$i] = $t[$i];
    }

    $outfile*=*fopen($file,*"w");
    for ($l=0;$l<$total_links;$l++) {
    fputs($outfile,*$new_array[$l]."\n");
    **** }
    fclose($outfile);

    echo "success..";
    ?>


    ok.. here is the exact code i am using its giving me a parser error, and i cant find where it is...

    Thanks a lot for the help so far.

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly does the error say?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the error says,
    Parse error: parse error in /home/unreg/public_html/index.php on line 17

    but i cant find anything wrong with it
    Last edited by unregistered1; Aug 15, 2001 at 21:57.

  15. #15
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm.. weird I just tried your exact code and it worked as expected. No parse error. Except here it adds an extra line break between each link because you need to use chop() when assigning the lements to the new array like.

    $new_array[$i] = chop($t[$i]);


    What platform are you running php on?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  16. #16
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    red hat linux.

  17. #17
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure you are viewing the most current copy of the page, because like said I copy and pasted that exact code and it works for me.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  18. #18
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy, its the most currect version. I have tried it on 5 different red hat servers. I get the same error.

  19. #19
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  20. #20
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your not gonna believe this.. but when i copied the source from your page.. it worked... really freaky..


Bookmarks

Posting Permissions

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