Re: Remove blank lines from a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Al wrote:

On 5/22/2010 4:34 PM, Robert Cummings wrote:
Al wrote:
On 5/22/2010 1:02 PM, Robert Cummings wrote:
tedd wrote:
At 4:27 PM +0200 5/21/10, Anton Heuschen wrote:
So in the file it would look like (from the original file the user
uploads
that is)

1
2

3
4


5

6


but when the file is saved to the server it must look like


1
2
3
4
5
6
If that is all (i.e., removing double linefeeds), then this will do it:

$text_array = array();
$text_array = explode("\n\n", $input_text);
$output_text = implode("\n",$text_array);
Sorry tedd, this is broken. It doesn't solve problems with runs of
greater than 2 newlines which is even in the example :) I would use the
following instead which is also line break agnostic with final output in
the style for your system:

<?php

$data = preg_replace( "#[\r\n]+#", PHP_EOL, $input );

?>

Cheers,
Rob.
Rob: Your solution doesn't remove the blank lines [\r\n]+ use instead
[\r\n]{2,} So 2 or more becomes only 1.

In general, problem is trickier when the following are considered. #
means any number. 0, 1.....

some textEOL
#spacesEOL
more text

some text#spacesEOL
#spacesEOL
.... any number of these
#spacesEOL
more text

some textEOL
EOL
...any number of these
EOL
some text

The white space before the EOLs can also be tabs

Look at the solution I posted earlier. The trim() removes all the
white spaces
My solution worked well where spaces were not an issue. Your solution
breaks my more general solution. Although I did realize I should have
trimmed the final output since any empty lead lines will not be removed.
Please review and see why you're comment to use [\r\n]{2,} does not work
properly. Correcting for lead blank lines and handling spaces in a blank
line is also quite simple without having to use the heavy solution of
foreach:

<?php

$data = preg_replace( "#[\r\n]+[[:space:]]+[\r\n]+#", "\n", $input );
$data = preg_replace( "#[\r\n]+#", PHP_EOL, $input );
$data = trim( $input );

?>

Without benchmarking, I'm willing to bet this is faster and less memory
intensive than your foreach solution :)

Cheers,
Rob.

Ignoring the space and tabs, you're right, the + does it. One or more always reduces to one only.

But the second solution I provided above addresses all issues (in case you missed that it was another solution :)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux