Re: converting fields from a fixed length text file into an array
| From: | heinisch at creaction dot de | Date: | Mon, 07 May 2001 06:02:47 +0000 |
| Subject: | Re: converting fields from a fixed length text file into an array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-51694@lists.php.net to get a copy of this message | ||
At 06.05.01 23:28, you wrote:
: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:I´m a little "old style" an read about file() the first time. I haven´t used file yet. For your problem try this "old style method, which I use with success. $fp=fopen("filename.txt","r") while(! feof($fp)) {ABCDEFG HIJ KLMNOPYASFSEFAFSAFSDFFDS (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 KLMFIELD 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 Hello Jay
$buffer=fgets($fp,$qty_to_read);
$part[1]=substr($buffer,0,18);
$part[2]=substr($buffer,0,18);
....
}
fclose($fp);
You would like tohave an easier job with extracting your sentences(when the appear in a logical order)
if you use an array, where to chop them
$where_to_chop=array(18,25,36,72);
for ($i=0; $i < count($where_to_chop);$i++)
{
$part[$i]=substr($buffer,$where_to_chop[$i],$where_to_chop[$i+1]);
....
$i++;
}
HTH Oliver