www.webdeveloper.com
Results 1 to 3 of 3

Thread: Read file and modify line by line before echo ??

  1. #1
    Join Date
    Feb 2006
    Posts
    352

    Post Read file and modify line by line before echo ??

    Good day to you all,

    I'm working on a script wich permit me to read a file line by line and echo it.

    Here is my code now:

    PHP Code:

    <?php

    $console 
    $_GET["console"];
    $name $_GET["name"];


    $file fopen("info_gen.txt""r") or exit("Unable to open file!");
    //Output a line of the file until the end is reached
    while(!feof($file))
      {
      echo 
    fgets($file). "<br />";
      }
    fclose($file);
    ?>

    My problem is I need to be able to add or edit each and every line seperatly and differently before echo everyting.

    How can I do so ?

    Thanks!
    That's why we are not alone on earth, to help each other !
    Let's REbuild that for ourselves !

  2. #2
    Join Date
    Nov 2009
    Posts
    9

    An array may help

    I think you might be looking for an array. I've rewritten part of you code with an array. I have created a file on my server called info_gen.txt with the following 5 lines in it:

    line 1
    line 2
    line 3
    line 4
    line 5

    The while function adds each line to the array. Once the while is finished I then have it echo each individual item from the array. I have it change the second line from info_gen.txt in the array to Hello World, then echo it.

    Here is a live example using the code below. http://code.chadayers.org/helper/1.php

    Is this what you are looking for?


    Code:
    <?php
    
    $console = $_GET["console"];
    $name = $_GET["name"];
    
    $ar = array();
    
    $file = fopen("info_gen.txt", "r") or exit("Unable to open file!");
    //Output a line of the file until the end is reached 
    while(!feof($file))
      {
      $ar[] = fgets($file);
      }
    fclose($file);
    echo $ar[0]. "<br />";
    $ar[1] = "Hello World";
    echo $ar[1]. "<br />";
    echo $ar[2]. "<br />";
    echo $ar[3]. "<br />";
    echo $ar[4]. "<br />";
    ?>

  3. #3
    Join Date
    Feb 2006
    Posts
    352
    Thanks !
    It was exactly the kind of code I needed !
    Array !
    That's why we are not alone on earth, to help each other !
    Let's REbuild that for ourselves !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles