Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and a FLat File? 1

Status
Not open for further replies.

iteach2

Technical User
Sep 16, 2001
120
US
Gurus,

I have a flat file where each record looks like the record below: Each record is 256 characters

625012C0010010070897111605 NIKE AIR FORCE ONE 7871077 15OZ 0001000000319000000002085000001HITOP 000000010007870 100000101000000000 00120001004 001500000703270327 000 00250200O

I want strip out diffrent sections of the record and write them to another file.

I have a key/legend which states the characters 1-7 is the header; characteres 8-12 us the batch# and so on all the way to 256.

How can I go thru each record and do something like this;

Grab the data between chars 14-26, 80-83 and 87-92 then right it to a file with headers like

Category Price UPC
14-26 80-83 87-92
14-26 80-83 87-92

I know PHP has the substr function but I'm not sure how to implement in this case.

I would appreciate any comments or thoughts.[bigcheeks]

 
does the string really look like you post or is it all in one line? if it is as you post, what is the determiner for an end of record?
 
Thanks for your response jpadie. It is actually on one line; When looking at the file in notepad without wordwrap.
 
easy enough then. it's a combination of reading the file line by line and using substr

Code:
<?php
$file = 'path/to/filename.txt'; //enter path to filename
$fh = fopen($file, "rb");
while (false !== ($line=fread($fh, 1024))){
	$items[] = parseLine($line);
}
echo "<pre>".print_r($items, true);
//you now have each line in an array element.

function parseLine($line){
	$category = substr($line, 14, 12);
	$price = substr($line, 80, 3);
	$UPC = substr($line, 87, 5);
	return array("category"=>$category, "price"=>$price,"UPC"=>$UPC);
}
?>
 
jpadie[bigsmile] this is exactly what I needed. I appreciate and thank you for your time and effort. Looking at the code I can understand what is happening. I have tried to run twice and it
is hanging. Nevertheless, I will troubleshoot from here. Thanks again for pointing me in the right direction. [peace]
 
no worries. if it is hanging it could be a timeout or similar (if the file is very long).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top