Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    United States
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Going through a txt file

    Could someone please show me a small example of how to go through every line of a txt file and to show the information for each line. Alls I need is the code to go through the txt file

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The best way to show line per line the contents of a file is using the file(); function which returns an array where each element of this array contains a line of the file.

    lines.txt :
    Line 1
    Line 2
    Line 3
    Line 4
    PHP Script :
    PHP Code:
    $lines file("lines.txt");
    foreach(
    $lines as $data){
            echo 
    $data "<br>";
    }
    // You can also directly input the text file in the foreach function :
    foreach(file("lines.txt") as $data)
            echo 
    $data ''<br>"; 
    The result would look exaclty like the text file.
    You can also use int readfile(string file); to read and print out the whole content of the file which is better is you just want to write out the whole textfile rather than doing line per line manipulation. (It returns the number of characters read.)
    I don't suffer from insanity, I enjoy every single minute of it!

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    United States
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you


  •  

    Posting Permissions

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