Announcement

Collapse
No announcement yet.

How to get rid of excess line breaks

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get rid of excess line breaks

    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 Code:
    <?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 Code:
    <?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.
    Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.
Working...
X