Jump to content
MakeWebGames

How to get rid of excess line breaks


Floydian

Recommended Posts

I'm sure most of us have run across a file on a server that has had lots of line breaks inserted in it. I've seen it happen a lot when moving a file from one folder to another. cpanel seems to be pretty bad with this. (that's in my humble opinion though, based on experience)

Whilst this isn't a major problem, I thought I'd share a little script I made that gets rid of some of those line breaks. Since we do want to preserve the line breaks that were in the file originally, we'll do a selective removal of the line breaks.

The basic code goes like this:

 

<?php

$file = file('some_file.php');
echo '<pre>';
$count = 0;
foreach ($file as $line_num => $line) {
if (strlen(str_replace("\n", "", $line)) < 1) {
	if ($count % 2) {
		$line = str_replace("\n", "", $line);
	}
	$count++;
}
echo htmlentities($line);
}
echo '</pre>';

 

That will print to the screen your file with the extra line breaks removed. Simply copy and paste it into your "exploded" file. I affectionately refer to files affected by these extra line breaks as exploded files. ^__^

If your file has been exploded more than once, you can run this script as many times as needed, copying and pasting the code each time. Or you can add in extra for loops. The for loop has to be a bit different, so to demonstrate:

 

<?php

$file = file('some_file.php');
echo '<pre>';

### THIS IS THE LOOP YOU CAN COPY ###
$count = 0;
foreach ($file as $line_num => &$line) {
if (strlen(str_replace("\n", "", $line)) < 1) {
	if ($count % 2) {
		$line = str_replace("\n", "", $line);
	}
	$count++;
}
}
unset($line);
### END OF THE THIS IS THE LOOP YOU CAN COPY ###

$count = 0;
foreach ($file as $line_num => $line) {
if (strlen(str_replace("\n", "", $line)) < 1) {
	if ($count % 2) {
		$line = str_replace("\n", "", $line);
	}
	$count++;
}
echo htmlentities($line);
}
echo '</pre>';

 

If your file had been exploded three times, you would copy that middle section one time, so that you'd have two of those sections. If you need to clear out a quadruply exploded file, copy that center section twice.

Of course this script exposes the code contained in whatever file you put in on this line -- $file = file('some_file.php'); --

So if you have top secret super sensitive code, keep that in mind.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...