Solved

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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.

746 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

9 Experts available now in Live!

Get 1:1 Help Now