Jump to content

Creating file in specific file format (with X amount of spaces, X letters, etc)


youngstarz

Recommended Posts

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!

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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...)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.