Solved

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

Posted on 2014-02-26
3
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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