Re: Remove blank lines from a file

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

 



At 1:02 PM -0400 5/22/10, Robert Cummings wrote:
tedd wrote:

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:

It's not broken according to my "given", which was "If that is all (i.e., removing double linefeeds), then this will do it:" My code does exactly what was stated. I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address.

Also, the solution you provided works better this way:  :-)

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

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
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