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…


format of the filename:


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] 


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

[ID] [name] [date]


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?


Now to build it yourself.
To get the file name,
You can use this to loop through a directory.

Now that you have the file names use explode by space
If there will always be three values you need I would use the “list” example.

To get the contents of the file use,
I just use file_get_contents(‘somefile.txt’)

Since you will have several entries in in your file you will need to explode by
(newline) and use the 1st array example
Here you will need another loop (inside the readdir loop)

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]

 [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.


uhm…im still new to php Sir…

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

So, what would be the value is the date was February 11th?

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?