youngstarz Posted February 4, 2012 Share Posted February 4, 2012 Hey everyone, I'm pretty new to this, not my full time job, but just something I thought I'd give a shot... I have a database, in postgres, in which I make my query and I go fetch all the info I need. What I need to do next is the tricky part for me. For each line of results received, I have to output to a text file (.txt) but I have to respect a format that was given to me from the person requesting the info. Example: Character 1 must be G or N. Character 2 to to 11 will be a result from my database search, which is a 10 digit string. Character 12 to 14 must be spaces. Another rule is: start at character 78: input a value from my database search but it must not exceed 20 characters and if it is less then 20 character, fill the remaining with spaces. If anyways has a code I can copy and work off of I would really appreciate it....THX! Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/ Share on other sites More sharing options...
Andy-H Posted February 4, 2012 Share Posted February 4, 2012 // do sql stuff $row[0] = (strlen($row[0]) > 10) ? substr($row[0], 0, 10) : str_pad($row[0], 10, '0'); $out = str_pad('', 78); $out .= 'G'; // or N $out .= str_pad($row[0], 3); Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/#findComment-1314291 Share on other sites More sharing options...
kicken Posted February 4, 2012 Share Posted February 4, 2012 sprintf may be a good function for this. Example: sprintf("%c%10s ", $firstChar, $digitString); You would set $firstChar to G or N based on whatever criteria controls that. $digitString is the 10 digit number. The format code should pad it with space if it is less than 10 digits. Then three spaces to fill out to char 14. You don't say what would go in the slot held by 15-77 so I stopped there but depending on the rules you may be able to insert them with other format codes. Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/#findComment-1314300 Share on other sites More sharing options...
DavidAM Posted February 4, 2012 Share Posted February 4, 2012 sprintf("%c%10s ", $firstChar, $digitString); I, too, recommend sprintf(). However, be advised that the "%10s" will not stop the string at 10 characters. It will pad the string if $digitString is less than 10 characters long; but if it is more than 10 characters long, it will include the entire string. So, it would be safer as: sprintf("%c%10s ", $firstChar, substr($digitString, 0, 10)); Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/#findComment-1314317 Share on other sites More sharing options...
youngstarz Posted February 5, 2012 Author Share Posted February 5, 2012 Great it works. Thx guys. On to my next question; As I get all my results, I convert everything to uppercase and send it to a text file. now my text file is all uppercase except for the é, à, etc... anyway of having all of those converted to E, A, etc ? (or maybe e, a, and then I put it in uppercase like the rest...) Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/#findComment-1314599 Share on other sites More sharing options...
youngstarz Posted February 5, 2012 Author Share Posted February 5, 2012 Found my answer, here it is if anyone ever searches: $newstring = iconv('UTF-8', 'ASCII//TRANSLIT', $oldstring); Quote Link to comment https://forums.phpfreaks.com/topic/256360-creating-file-in-specific-file-format-with-x-amount-of-spaces-x-letters-etc/#findComment-1314643 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.