php parse text file 5 fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eian@php
Forum Newbie
Posts: 7
Joined: Sat Aug 21, 2010 10:22 am

php parse text file 5 fields

Post 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.. :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php parse text file 5 fields

Post 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)
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php parse text file 5 fields

Post by requinix »

If each and every space is a field delimiter then a simple str_replace would be enough.
Post Reply