Solved

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

Posted on 2014-02-26
3
366 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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now