Page 1 of 1

php parse text file 5 fields

Posted: Sat Aug 21, 2010 10:40 am
by eian@php
hi i have a text file with 5 fields per line.. i want to parse these fields and save it to mysql database table

this is the format of my text file:

joe 1234asdc 7:51:00 2010-08-21 hi
john 543kjkj 7:45:00 2010-08-21 hello
rey 675jhgj 7:40:00 2010-08-21 nice

the fields are separated by spaces.. anyone help me on how to do the exact coding.. :)

Re: php parse text file 5 fields

Posted: Sat Aug 21, 2010 12:21 pm
by AbraCadaver

Code: Select all

$lines = file('/path/to/file.txt');

foreach($lines as $line) {
   $values = "'".implode("','", explode(' ', $line))."'";
   // INSERT INTO table_name (field1, field2, field3, field4, field5) VALUES ($values)
}

Re: php parse text file 5 fields

Posted: Sat Aug 21, 2010 10:30 pm
by requinix
If each and every space is a field delimiter then a simple str_replace would be enough.