Solved

php fprintf

Posted on 2015-01-29
5
63 Views
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.
output-file.png
Unit04Lab.php
0
Comment
Question by:cookiejar
  • 3
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
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.
http://php.net/manual/en/function.sprintf.php

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

Author Comment

by:cookiejar
ID: 40578500
Currently the last two columns look like this:
SALES DIFF               MESSAGE
   -5THIS IS THE MESSAGE TEXT

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

I would like to have the final column, right-justified.
0
 
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

0
 

Author Comment

by:cookiejar
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:

   Message

   The message column text
0
 
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 ...

	
sprintf($strSalesDiff,"%3d",$salesDiff);
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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