converting fields from a fixed length text file into an array

From: Date: Mon, 07 May 2001 03:28:15 +0000
Subject: converting fields from a fixed length text file into an array
Groups: php.general 
Request: Send a blank email to php-general+get-51689@lists.php.net to get a copy of this message
:Please forgive me for having posted this more than once. I am trying to reword it here to make it more plain. --------------- I have a fixed length text file I'm trying to import and send into MySQL. A record line is about 1600 characters long and includes many blank spaces. I thought by using file('FILENAME.TXT') I would have a nice neat array of each record in the file based on each line being a record. Here's an example txt file: ABCDEFG HIJ KLMNOP YASFSEFAFSAFSDFFDS (notice the 4 spaces after J and before K on line 1 of the above text file) So, I went on breaking up the line into fields by doing this: $ra=$recordArray=file('FILENAME.TXT'); for($index=0;$index<count($ra);$index++) { $FIELD_1[]=substr($ra[$index],0,18); $FIELD_2[]=substr($ra[$index],18,19); $FIELD_3[]=substr($ra[$index],19,20); } I expected that to give me 3 fields on line 1 that would have looked like this: FIELD 1 = ABCDEFG HIJ KLM FIELD 2 = N FIELD 3 = OP Instead it gave me: FIELD 1 = ABCDEFG HIJ KLMOP FIELD 2 = FIELD 3 = because it stripped out the empty spaces along the way. Can you tell me how I might prevent that from happening? Thanks very much, Jay Lepore jay@pineisland.net

« previous php.general (#51689) next »