SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parse filename and contents of textfile

    hi to all...

    does anybody has an idea of parsing the filename of the textfile and its contents and insert to database..

    example..

    format of the filename:
    [date] [code] [time]

    201089 1-111 93300.txt


    contents of the text file:

    12345 John Robinson
    54321 Robert svenson


    the table where the information should be inserted has this
    fields.. name of table: logs

    [ID] [name] [date] [code] [time]

    say for example the parsing is done..it should look like this.


    [ID] [name] [date] [code] [time]

    12345 John Robinson 2010-8-9 1-111 9:33:00
    54321 Robert Svenson 2010-8-9 1-111 9:33:000


    how can i do that??
    can somebody help me?

    thanks.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    What happens with potentially confusing values? For example, if the "date" was 2010112 is that the first of December, or 11th of February?

    How familiar are you with using a database, and using PHP?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhm..im still new to php Sir..

    if the date is 2010112.. it would be record as the january 12, 2010

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    So, what would be the value is the date was February 11th?
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Now to build it yourself.
    To get the file name,
    http://php.net/manual/en/function.readdir.php
    You can use this to loop through a directory.

    Now that you have the file names use explode by space
    http://www.php.net/manual/en/function.explode.php
    If there will always be three values you need I would use the "list" example.

    To get the contents of the file use,
    http://www.php.net/manual/en/functio...t-contents.php
    I just use file_get_contents('somefile.txt')

    Since you will have several entries in in your file you will need to explode by \n (newline) and use the 1st array example
    Here you will need another loop (inside the readdir loop)
    http://www.php.net/manual/en/control...es.foreach.php

    Where you will do yet another explode by space and again, I would use the 2nd list example. Don't forget the first and last name are separated by a space.

    Here is where you would write to the database.

    You have the first [date] [code] [time] values from the first explode by space then here you would have [ID] [name] from the last explode by space.

    Since the first explode is outside of the /n explode those values will stay the same and on the next loop it will get the next [ID] [name] values.

    If you are new to PHP and sounds like you want to learn, this will give you the planning and the links provided will pretty much build it for you.

    Cheers
    Loren
    What I lack in acuracy I make up for in misteaks


Bookmarks

Posting Permissions

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