Solved

PHP - fwrite / str_pad - where am i going wrong?!

Posted on 2014-02-26
3
376 Views
Last Modified: 2014-02-26
I currently have a text file that needs to be formatted in a certain way (each line must be 100 chars total) - if the data isn't 100 chars, it should be padded with spaces... So, this is the code I have at the moment - i'm reading a file line by line, padding it out and writing it back to another file...

 while(!feof($file))
   {
	   	$LineData = fgets($file);
		$PaddedLineData = str_pad($LineData, 100," ",STR_PAD_RIGHT);
		$FinalLineData = $PaddedLineData . "\r\n";
   		fwrite($newfile, $FinalLineData);
   }

Open in new window


That ALMOST works..... this is what the new txt file looks like:


line of data 1
(correct amount of padding for line 1)
line of data 2
(correct amount of padding for line 2)

-the padding is on the second / fourth line - not the first and second - it is however, the correct amount of padding...

Can anyone suggest how to fix this and get the padding on the correct lines?

Thanks
0
Comment
Question by:Bergstr
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39888633
Ditch the end lines.

$FinalLineData = $PaddedLineData . "\r\n";
should be
$FinalLineData = $PaddedLineData;

Reason: fgets reads the line including end-line characters.

HTH,
Dan
0
 

Author Comment

by:Bergstr
ID: 39888650
hmm... that's probably my problem... when im reading the lines from the file, its adding a new line - so at this stage:

$PaddedLineData = str_pad($LineData, 100," ",STR_PAD_RIGHT);

i'm adding the padding after the new line, which is the output i'm getting (and don't want).

Is there a way to stop it from reading the new line data (or remove it from the string)

Thanks
0
 

Author Comment

by:Bergstr
ID: 39888672
Got it :)

changed fgets for stream_get_line and I now have the output I'm after:


Final code:

 while(!feof($file))
   {
               $LineData = stream_get_line($file, 1000000, "\n");
            $PaddedLineData = str_pad($LineData, 100," ",STR_PAD_RIGHT);
            $FinalLineData = $PaddedLineData . "\r\n";
               echo $FinalLineData . "<br>";
               fwrite($newfile, $FinalLineData);
   }


Thanks for the help / pointing me in the right direction :)
0

Featured Post

Industry Leaders: 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

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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 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