read line from text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lucas

    read line from text file

    I have a text file that i would like to use PHP to read from. each line
    ends with a \n. i need to read from the beginning of the line, only upto
    that \n
    right now i'm using this, which is sloppy and slow:

    if ($chr = fgetc($file)) != "\n") { }

    which of course reads in one byte at a time till the end of line. is there
    a better way?
    --
    lucas
    -------------------------
    Perl Coder since 2001
    shift || die;
    -------------------------
  • nc@iname.com

    #2
    Re: read line from text file

    lucas wrote:[color=blue]
    >
    > I have a text file that i would like to use PHP to read from. each[/color]
    line[color=blue]
    > ends with a \n. i need to read from the beginning of the line, only[/color]
    upto[color=blue]
    > that \n
    > right now i'm using this, which is sloppy and slow:
    >
    > if ($chr = fgetc($file)) != "\n") { }
    >
    > which of course reads in one byte at a time till the end of line. is[/color]
    there[color=blue]
    > a better way?[/color]
    Yes; read up on fgets():



    Cheers,
    NC

    Comment

    • Pedro Graca

      #3
      Re: read line from text file

      nc@iname.com wrote:[color=blue]
      > lucas wrote:[color=green]
      >> if ($chr = fgetc($file)) != "\n") { }[/color][/color]
      [color=blue]
      > http://www.php.net/fgets[/color]

      If you need to process all lines in the file (and the file isn't
      extraordinarily big) also consider


      http://www.php.net/file_get_contents (for PHP >= 4.3.0)

      --
      Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      == ** ## !! ------------------------------------------------ !! ## ** ==
      TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      may bypass my spam filter. If it does, I may reply from another address!

      Comment

      • lucas

        #4
        Re: read line from text file

        nc@iname.com wrote:

        [color=blue]
        > Yes; read up on fgets():[/color]

        Thanks. I read about that one before, but the manual says you need to
        specify x amount of bytes, after you mentioned it, i checked it closer, and
        the size was optional. thanks much. sped up my script from 12 secs
        to .5 ;)
        --
        lucas
        -------------------------
        Perl Coder since 2001
        shift || die;
        -------------------------

        Comment

        • jblanch

          #5
          Re: read line from text file

          you could also read the entire file into one string, then use explode()
          and the \n delimiter. Its very low-level and childish, but works
          effectivly and is easy to understand and work with..

          Ex:
          $filename = "yourfile.t xt";
          $handle = fopen($filename , "r");
          $contents = fread($handle, filesize($filen ame));
          fclose($handle) ;

          $lines = explode("\n",$l ines);
          foreach($lines as $line){
          // line is stored in $line
          }

          Comment

          • Daniel Tryba

            #6
            Re: read line from text file

            jblanch <jblanch@gmail. com> wrote:[color=blue]
            > $filename = "yourfile.t xt";
            > $handle = fopen($filename , "r");
            > $contents = fread($handle, filesize($filen ame));
            > fclose($handle) ;
            >
            > $lines = explode("\n",$l ines);
            > foreach($lines as $line){
            > // line is stored in $line
            > }[/color]

            Never heard of file()?

            Comment

            • jblanch

              #7
              Re: read line from text file

              hm, guess file works too eh? but its the same idea, just without the
              fopen/ect functions.

              Comment

              • Daniel Tryba

                #8
                Re: read line from text file

                jblanch <jblanch@gmail. com> wrote:[color=blue]
                > hm, guess file works too eh? but its the same idea, just without the
                > fopen/ect functions.[/color]

                file() works for any textfile, you example is only for unix type of
                files. DOS type will have \r cluttering the end of lines. Mac files will
                appear to contain 1 single line.

                You could fix it by using preg_split("/(\r\n|\r|\n)/",$line) instead of
                explode("\n",$l ine).

                BTW is it really so hard to keep some context in your postings?

                Comment

                • jblanch

                  #9
                  Re: read line from text file


                  Daniel Tryba wrote:[color=blue]
                  > jblanch <jblanch@gmail. com> wrote:[color=green]
                  > > hm, guess file works too eh? but its the same idea, just without[/color][/color]
                  the[color=blue][color=green]
                  > > fopen/ect functions.[/color]
                  >
                  > file() works for any textfile, you example is only for unix type of
                  > files. DOS type will have \r cluttering the end of lines. Mac files[/color]
                  will[color=blue]
                  > appear to contain 1 single line.
                  >
                  > You could fix it by using preg_split("/(\r\n|\r|\n)/",$line) instead[/color]
                  of[color=blue]
                  > explode("\n",$l ine).
                  >
                  > BTW is it really so hard to keep some context in your postings?[/color]

                  I'm sorry, but correct me if i'm wrong, but didn't he state that he
                  needs to read to \n? yes, i'm sure he did. there is nothing wrong
                  with the method i described, and i'm 100% sure it would work for him.
                  Hows that for context?

                  Comment

                  Working...