I need to build a system that can generate flat-text files with items in fixed positions.
for example:
First Name: position 1 - 99
Last Name: position 100 - 199
Phone Num: position 200-220
etc.
I'd like some suggestions as to what you think would be the best way to tackle this. My first concern is that the length of names, phone numbers etc is going to be variable and I need to account for those differences. I'm not sure of a php function that will allow me to continue a string at a pre-defined placement but if there is such a thing...that would be my golden key. My alternative (as I see it) is to figure out the length of the item, then add spaces until I hit the next spot which is not the option I want to pursue as there are a TON of 'fields' that I need to account for.
If any of you have done something like this, or know of a good way to tackle this project, please share as I really don't want to have to 'hard-code' this for all of the fields that are in this monstrosity.
need to create a fixed placement flat-file
Moderator: General Moderators
Code: Select all
printf("[%-100s][%-200s][%-50s][%-100s]",$strVariable1,$strVariable2,$strVariable3,$strVariable4);Edit: If you wanted to print on a line from a specific location..
Code: Select all
function printat($line, $position, $string) {
if (strlen($line)<$position) {
$line.=str_repeat(" ",$position-$line);
}
$line.=$string;
}
Last edited by onion2k on Tue Dec 20, 2005 4:59 pm, edited 1 time in total.
dbase is fixed length, but just make the field lengths larger then you need them, or when you do need to go over that length, create a new flatfile with the new field length, delete the old one, copy the new one back to where the old one was
http://us3.php.net/dbase
For variable length flatfiles, I would either recommend an xml file, or for a "real" flatfile approach, just have a different file for each field, with each record having a corresponding line in each file
You also might want to check out isam flatfiles, I think they even support indexing
http://us3.php.net/dbase
For variable length flatfiles, I would either recommend an xml file, or for a "real" flatfile approach, just have a different file for each field, with each record having a corresponding line in each file
You also might want to check out isam flatfiles, I think they even support indexing
onion2k -
I'll give that a whirl tomorrow as I think it looks like its headed in the direction I need to go.
a sample might be (and this would be data I'm selecting off of a MySQL db).
as you can see, the variable sizes are not the same but the placement of the next 'item' must be the same. I assume your solution would work and as I said I will tackle it tomorrow and let you know.
thx for the input,
Burr
I'll give that a whirl tomorrow as I think it looks like its headed in the direction I need to go.
a sample might be (and this would be data I'm selecting off of a MySQL db).
Code: Select all
Bob Jones 888-888-8888 444-44-4444 1975-12-22 Married
Larry Smith 777-777-7777 333-33-3333 1964-11-02 Singlethx for the input,
Burr