Read lines from text file using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fab108
    New Member
    • Jun 2007
    • 16

    Read lines from text file using PHP

    Hello everyone, i am new here. Hmm...can you all please help me with this..what is the code for reading lines from a text file..Thanks =)
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Originally posted by Fab108
    Hello everyone, i am new here. Hmm...can you all please help me with this..what is the code for reading lines from a text file..Thanks =)
    Hi, Fab108 Welcome to TSDN.

    Try these Tutorials

    Comment

    • Fab108
      New Member
      • Jun 2007
      • 16

      #3
      Thanks for your help, but fgets help us to get the first line for example my text file is

      Datalogs:
      Date: 2007/6/11
      Time: 13:9:3
      Device ID: B108
      About total Numbers: 001891
      =============== ====
      =============== ======
      Record: 1
      Event Type: 3
      Year: 2007
      Month: 6
      Day: 7
      Hour: 5
      Minute: 9
      Second: 37.527237


      And i only want to display from Year onwards, how do i do that using fgets?

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by Fab108
        Thanks for your help, but fgets help us to get the first line for example my text file is

        Datalogs:
        Date: 2007/6/11
        Time: 13:9:3
        Device ID: B108
        About total Numbers: 001891
        =============== ====
        =============== ======
        Record: 1
        Event Type: 3
        Year: 2007
        Month: 6
        Day: 7
        Hour: 5
        Minute: 9
        Second: 37.527237


        And i only want to display from Year onwards, how do i do that using fgets?
        I didn't ask to use just only the fgets().There were number of tutorials on the search result that i supplied.
        And one more thing if you want to read this values from a text file there should be a specific format for each and every record.

        example:
        [code=text]
        Mike Johann | 15-02-2005 | mike@somewhere. com
        Jessica Moore | 20-10-2000 | jess@elesewhere .com

        [/code]
        for better readability.

        The format that you have supplied also can read, but its not a god practice for creating a application. Still i can't understand whether it is a single file or a combination. and is this the only record you have?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Fab108
          Thanks for your help, but fgets help us to get the first line for example my text file is

          Datalogs:
          Date: 2007/6/11
          Time: 13:9:3
          Device ID: B108
          About total Numbers: 001891
          =============== ====
          =============== ======
          Record: 1
          Event Type: 3
          Year: 2007
          Month: 6
          Day: 7
          Hour: 5
          Minute: 9
          Second: 37.527237


          And i only want to display from Year onwards, how do i do that using fgets?
          Use the gets function then .

          Comment

          • Fab108
            New Member
            • Jun 2007
            • 16

            #6
            Originally posted by ajaxrand
            I didn't ask to use just only the fgets().There were number of tutorials on the search result that i supplied.
            And one more thing if you want to read this values from a text file there should be a specific format for each and every record.

            example:
            [code=text]
            Mike Johann | 15-02-2005 | mike@somewhere. com
            Jessica Moore | 20-10-2000 | jess@elesewhere .com

            [/code]
            for better readability.

            The format that you have supplied also can read, but its not a god practice for creating a application. Still i can't understand whether it is a single file or a combination. and is this the only record you have?

            No i got 1891 records..= (..i am new to php that is why i am so vex now..haha

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Is this the structure of your text file or differ than this?

              [CODE=text]Datalogs:
              Date: 2007/6/11
              Time: 13:9:3
              Device ID: B108
              About total Numbers: 001891
              =============== ====
              =============== ======
              Record: 1
              Event Type: 3
              Year: 2007
              Month: 6
              Day: 7
              Hour: 5
              Minute: 9
              Second: 37.527237
              Record: 2
              Event Type: 3
              Year: 2007
              Month: 6
              Day: 7
              Hour: 5
              Minute: 9
              Second: 37.527237
              Record: 3
              Event Type: 3
              Year: 2007
              Month: 6
              Day: 7
              Hour: 5
              Minute: 9
              Second: 37.527237[/CODE]

              And what you meant by saying "year onwards" ?

              Comment

              • Fab108
                New Member
                • Jun 2007
                • 16

                #8
                Datalogs:
                Date: 2007/6/11
                Time: 13:9:3
                Device ID: Data Logger
                About total Numbers: 001891
                =============== ====
                =============== ======
                Record: 1
                Event Type: 3
                Year: 2007
                Month: 6
                Day: 7
                Hour: 5
                Minute: 9
                Second: 37.527237
                Latitude: 1.342335
                Longitude: 103.776921
                Altitude: 0.000000
                PDOP: 50.000000
                HDOP: 50.000000
                Satellite No: 3
                Speed(KMs/hr): 0.000000
                Direction: 0.000000
                =============== ======
                Record: 2
                Event Type: 1
                Year: 2007
                Month: 6
                Day: 7
                Hour: 5
                Minute: 9
                Second: 42.526936
                Latitude: 1.342367
                Longitude: 103.776861
                Altitude: 0.000000
                PDOP: 50.000000
                HDOP: 50.000000
                Satellite No: 3
                Speed(KMs/hr): 0.000000
                Direction: 0.000000
                =============== ======
                Record: 3
                Event Type: 1
                Year: 2007
                Month: 6
                Day: 7
                Hour: 5
                Minute: 9
                Second: 47.526634
                Latitude: 1.342375
                Longitude: 103.776858
                Altitude: 0.000000
                PDOP: 50.000000
                HDOP: 50.000000
                Satellite No: 3
                Speed(KMs/hr): 0.000000
                Direction: 0.000000
                =============== ======

                This is the text file, from record 1, all the way to 1891.

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  text.txt
                  [CODE=text]
                  Datalogs:
                  Date: 2007/6/11
                  Time: 13:9:3
                  Device ID: Data Logger
                  About total Numbers: 001891
                  =============== ====
                  =============== ======
                  Record: 1
                  Event Type: 3
                  Year: 2007
                  Month: 6
                  Day: 7
                  Hour: 5
                  Minute: 9
                  Second: 37.527237
                  Latitude: 1.342335
                  Longitude: 103.776921
                  Altitude: 0.000000
                  PDOP: 50.000000
                  HDOP: 50.000000
                  Satellite No: 3
                  Speed(KMs/hr): 0.000000
                  Direction: 0.000000
                  =============== ======
                  Record: 2
                  Event Type: 1
                  Year: 2007
                  Month: 6
                  Day: 7
                  Hour: 5
                  Minute: 9
                  Second: 42.526936
                  Latitude: 1.342367
                  Longitude: 103.776861
                  Altitude: 0.000000
                  PDOP: 50.000000
                  HDOP: 50.000000
                  Satellite No: 3
                  Speed(KMs/hr): 0.000000
                  Direction: 0.000000
                  =============== ======
                  Record: 3
                  Event Type: 1
                  Year: 2007
                  Month: 6
                  Day: 7
                  Hour: 5
                  Minute: 9
                  Second: 47.526634
                  Latitude: 1.342375
                  Longitude: 103.776858
                  Altitude: 0.000000
                  PDOP: 50.000000
                  HDOP: 50.000000
                  Satellite No: 3
                  Speed(KMs/hr): 0.000000
                  Direction: 0.000000
                  =============== ======
                  [/CODE]

                  Try to modify this script as per your requirement.
                  read_text.php
                  [CODE=php]
                  <?php
                  $filename = "text.txt";
                  $fd = fopen ($filename, "r");
                  $contents = fread ($fd,filesize ($filename));
                  fclose ($fd);

                  $delimiter = "============== =======";
                  $splitcontents = explode($delimi ter, $contents);

                  foreach( $splitcontents as $record )
                  {

                  echo "$record<hr >";
                  }

                  ?>
                  [/CODE]

                  Comment

                  • Fab108
                    New Member
                    • Jun 2007
                    • 16

                    #10
                    Thanks a lot, if i have anymore problem, i will pm you..= )

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      Originally posted by Fab108
                      Thanks a lot, if i have anymore problem, i will pm you..= )
                      Glad to hear that, Fab108 !
                      You are welcome ! anytime.
                      -Ajaxrand

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Fab108
                        Thanks a lot, if i have anymore problem, i will pm you..= )
                        Unfortunately we don't allow technical questions to be asked through PMs. we prefer it for them to be posted here where we all can learn from them.

                        Sorry for being a spoil sport but those are the rules.

                        Comment

                        • ak1dnar
                          Recognized Expert Top Contributor
                          • Jan 2007
                          • 1584

                          #13
                          Originally posted by r035198x
                          Unfortunately we don't allow technical questions to be asked through PMs. we prefer it for them to be posted here where we all can learn from them.

                          Sorry for being a spoil sport but those are the rules.
                          Its my mistake, i really forgot to mention that in my last post.

                          Do NOT PM questions to individual Experts, Moderators or Administrators

                          Sorry r035198x.

                          Comment

                          • Fab108
                            New Member
                            • Jun 2007
                            • 16

                            #14
                            Ok i understand, haha i will post my questions if i have any = ) thanks a lot.

                            Comment

                            • Fab108
                              New Member
                              • Jun 2007
                              • 16

                              #15
                              Ok..i got a new problem. After i read the lines, how i convert the data and put into a database?

                              Comment

                              Working...