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 14 of 14
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    textfile read specified line(s)

    how using php can i read a specified line/lines of a text file. an example i would like to read lines 1-4 and output them to a page. another example would be read line 5 and output it to a page.

    any ideas?

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    with fopen() and fgets() (or fread() if you develop cross-platform stuff). fgets read one line, so you can have a loop until the pointer is on the line you need.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    do you have any code for this? as i'm a little bit confused as i'm new to php.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <edit>filefunction (see below) is probably more efficinient way to get a speciffic line</edit>


    untested code
    PHP Code:
    <?php
    $line 
    0;
    $filehand fopen ("thefile.txt""r");
    while (!
    feof ($filehand)) {
      if (
    $line == 5) {
        echo 
    fgets($filehand4096);
        break;
      }
      else {
        
    $line ++
      }
    }
    fclose ($filehand);
    ?>
    more info on php.net
    http://www.php.net/manual/en/function.fgets.php
    Last edited by raf; 04-20-2003 at 09:22 AM.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks raf your code explains it a lot better. I did read the manual section on it earlier and that was what left me a little confused.

    thanks and happy easter.
    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Look at the "file" function. It converts each line as a seperate element in an array (in this case $array)


    PHP Code:
    $array file($file_name);
    echo 
    $array[0]; //first line
    echo $array[1]; //second line
    echo $array[2]; //third line
    echo $array[3]; //fourth line 
    Those should be your first four lines.


    Later
    Duniyadnd

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    duniyadnd thanks for that it could prove very useful later.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    I have decided to use the following code
    PHP Code:
    $line == 0;
    $array file($file_name);
    //need a loop system while not end of file
    $line++;
    echo 
    $array[$line]; 
    if i use while or another method how can i detect the end of the file? so that htere are no more lines left to read?

    which is the correct syntax to use as a loop with the condition that its not the end of the file? to go in the space of the cooment.
    scroots
    Last edited by scroots; 04-22-2003 at 09:54 PM.
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function feof( resource handle) does that, but so does this
    while (!feof ($filehand))

  • #10
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Try mysql?

    I think you should try mysql..

    I am willing to teach you if you want. just go on msn and I will teach you step by step.

    I am offering you this cuz I was a newbie once, it was hard...

    let me know if you want to..

    Mike

  • #11
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by scroots
    I have decided to use the following code
    PHP Code:
    $line == 0;
    $array file($file_name);
    //need a loop system while not end of file
    $line++;
    echo 
    $array[$line]; 
    if i use while or another method how can i detect the end of the file? so that htere are no more lines left to read?

    which is the correct syntax to use as a loop with the condition that its not the end of the file? to go in the space of the cooment.
    scroots
    You are looping through the array, not the file.
    So:
    PHP Code:
    $array file($file_name);
    for(
    $line=0;$line<sizeof($array);$line++);
    echo 
    $array[$line]; 
    Also, if you want to print last line only:
    PHP Code:
    $array file($file_name);
    echo 
    $array[sizeof($array)-1]; 
    WBR, Weirdan.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    suppose so.

    I will try it out, but wouldn't an error be produced if it looks for part of the array that doesn't exist? e.g. it looks for arrayline number nine in an eight lined text file.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No error, as it looks at the last element minus one. You're not looking at the file, you looking at the array, so you only concerned with the array length, which you got already, by using the sizeof function (personally, i use the count function, but they both get the job done).

    Duniyadnd

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks very much.

    it will prove very useful and it has solved my current problem.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished


  •  

    Posting Permissions

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