php fprintf

Posted on 2015-01-29
Last Modified: 2015-02-12
I am reading an input file and am outputting it in a report format, using the fprintf function .  I have aligned all of the columns under the column headings with the exception of the last column.  

I am having problems aligning the last column. When I enter '%-3s', for example,  the value does not move. How is this done?
See the attachment for the expected output.
Question by:cookiejar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 32

Accepted Solution

Daniel Wilson earned 500 total points
ID: 40578422
In the PNG file you attached, the final column looks aligned correctly.  Are you meaning you want it right-justified instead?

Example 8 on this page may be helpful.

If not, please explain a little more completely where the problem is and I'll try to help.

Author Comment

ID: 40578500
Currently the last two columns look like this:
SALES DIFF               MESSAGE

Would like them to look like this:
     -5                        THIS IS THE MESSAGE TEXT

I would like to have the final column, right-justified.
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40578516
OK, how's this?
	fprintf($fout,"\n    %-11s%-12s%3d%25s",$itemID,$itemName,$salesDiff,$message);

Open in new window


Author Comment

ID: 40578818
I am sorry I misunderstood the question about the column being right-justified.  
I want the columns to align under the column header:


   The message column text
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40578864
And I misunderstood your answer.  Sorry about that.

I think we need to right-pad the SalesDiff column with spaces. Let's try ...

fprintf($fout,"\n    %-11s%-12s%20s%s",$itemID,$itemName,$strSalesDiff,$message);

Open in new window

That should create a properly formatted string that can then be properly spaced in the 2nd line.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question