Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-26
3
Medium Priority
?
418 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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