This is all really straight forward stuff.  Here's some code that will get the file 
parsed to an array, what you do from there is up to you.


$open =  fopen("file.txt","r+");

while($contents = fgets($open)) {
$line_array[$x] = $contents;


Note: if you have a precise structure for your lines you can say to read from point a 
to b using substr().  For example, let's say the first name is exactly 3 characters in 
from 0  and exactly 10 characters long so you could do substr($line_array[0], 3, 10);  
This is a really dirty way to handle this and I'm sure there are better ways so poke 
around a bit.  Also if you have your phone number located down to a specific location 
you can use an explode statement to get both area code and main number seperated.

----- Original Message -----
From: Jason D. Williard
Sent: Saturday, June 15, 2002 1:28 PM
Subject: [PHP] Parsing Text File

I am trying to input data from a text file into a MySQL database and would
like to be able to input the data using a single script.  What's the easiest
way to parse a line, such as below, and turn it into variables to be placed
in the database.  While I can simply place the data in by importing from a
file, it's not quite so easy.  I only need to place some of the data in,
plus I would like to split one of the fields.  Below is an example of a line
from the file, and then the variables that I need to enter.

Here is an example of a line from the file:
403 -770 -4904 ,CALGARY,AB,CAN,T2,Y,Y,Apr 29 2002

As for variables, I need the following:
Split Number into $AreaCode & $Number > 403 & 770-4904

Thanks for any help.

Jason D. Williard

PHP General Mailing List (
To unsubscribe, visit: more from the Web.  FREE MSN 
Explorer download :

Reply via email to