RabPHP Posted October 20, 2007 Share Posted October 20, 2007 Greetings all, I have to write a file format that requires some very specific data in specific positions on specific lines. For example... I have a list of data with the following requirements: First Name (Line 1 position 1) Last Name (Line 1 position 45) Street Address (Line 2 Position 1) City (Line 2 position 40) State (Line 2 position 55) Zip (Line 2 Position 58) Phone Number (Line 3 Position 1) Fax Number (Line 3 Position 11) I have tried a combination of fputs, fseek however fseek keeps going back to the first line. This will be a new file each time therefore the lines do not exist yet to put into an array. Any advise greatly appreciated. Rab Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/ Share on other sites More sharing options...
0x00 Posted October 20, 2007 Share Posted October 20, 2007 Do something like this, then write out line by line... $1 = $fname; $1 = str_pad($1, 44); $1 .= $lname; $2 = $street; $2 = str_pad($2, 39); $2 .= $city; $2 = str_pad($2, 54); $2 .= $state; $2 = str_pad($2, 57); $2 .= $zip; $3 = $tel; $3 = str_pad($3, 10); $3 .= $fax; However what would happen if I lived here 'Llandrindod Wells'? Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374091 Share on other sites More sharing options...
esukf Posted October 20, 2007 Share Posted October 20, 2007 Try <?php $formated = sprintf("%-44.44s", $firstname)."$lastname\n"; $formated .= sprintf("%-39.39s", $street).sprintf("%-15.15s", $city).sprintf("%-3.3s", $state)."$zip\n"; $formated .= sprintf("%-10.10s", $phone)."$fax\n"; ?> This will truncate any variable length longer than allowed. Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374101 Share on other sites More sharing options...
wildteen88 Posted October 20, 2007 Share Posted October 20, 2007 If you have multiple lists then you could use this: <?php $format = array( array(45, 20), array(40, 15, 3, 6), array(11, 12) ); function getLineData($linedata, $line) { global $format; $i = 0; foreach($format[$line] as $k => $offset) { $tmp[] = trim(substr($linedata, $i, $offset)); $i += $offset; } return implode("\n", $tmp); } $data = file('details.txt'); $tmp = null; for($i = 0; $i <= (count($data)-1); $i++) { for($k = 0; $k < 3; $k++) { $tmp .= "\n" . getLineData($data[$i++], $k); } $details[] = $tmp; $tmp = null; $i--; } foreach($details as $c_details) { list(,$firstnane, $lastname, $addr, $city, $state, $zip, $phone, $fax) = explode("\n", $c_details); echo "$firstnane $lastname<br /> $addr<br /> $city<br /> $state $zip<br /> $phone $fax <hr />\n"; } ?> details.txt format: First Name1 Last Name1 Street Address1 City ST Zip Phone #5 Fax Number1 First Name2 Last Name2 Street Address4 City ST Zip Phone #4 Fax Number2 First Name3 Last Name3 Street Address3 City ST Zip Phone #3 Fax Number3 Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374123 Share on other sites More sharing options...
esukf Posted October 20, 2007 Share Posted October 20, 2007 Try <?php $formated = sprintf("%-44.44s", $firstname)."$lastname\n"; $formated .= sprintf("%-39.39s", $street).sprintf("%-15.15s", $city).sprintf("%-3.3s", $state)."$zip\n"; $formated .= sprintf("%-10.10s", $phone)."$fax\n"; ?> Refactoring <?php $formated = sprintf("%-44.44s%s\n", $firstname, $lastname); $formated .= sprintf("%-39.39s%-15.15s%-3.3s%s\n", $street, $city, $state, $zip); $formated .= sprintf("%-10.10s%s\n", $phone, $fax); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374161 Share on other sites More sharing options...
RabPHP Posted October 20, 2007 Author Share Posted October 20, 2007 Greetings all, I have to write a file format that requires some very specific data in specific positions on specific lines. For example... I have a list of data with the following requirements: First Name (Line 1 position 1) Last Name (Line 1 position 45) Street Address (Line 2 Position 1) City (Line 2 position 40) State (Line 2 position 55) Zip (Line 2 Position 58) Phone Number (Line 3 Position 1) Fax Number (Line 3 Position 11) I have tried a combination of fputs, fseek however fseek keeps going back to the first line. This will be a new file each time therefore the lines do not exist yet to put into an array. Any advise greatly appreciated. Rab This works perfectly and allows me to seperate everything out nice and neat, thanks! Rab Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374221 Share on other sites More sharing options...
Ninjakreborn Posted October 20, 2007 Share Posted October 20, 2007 Nice responses. I see some really valuable code throughout this post. Quote Link to comment https://forums.phpfreaks.com/topic/74089-solved-writing-files/#findComment-374223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.