Re: Remove blank lines from a file
| From: | Nathan Rixham | Date: | Sun, 23 May 2010 20:57:48 +0000 |
| Subject: | Re: Remove blank lines from a file | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-305466@lists.php.net to get a copy of this message | ||
Robert Cummings wrote:
Nathan Rixham wrote:quote: 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 </quote> the above produces what's required; and hence the vertical whitespace only solution included too "/(\h)\h+/im" what version are you on btw? not being an ass and actually am interested in where it doesn't work (as I use the code on some important sites & in some apps that are open sourced) Best, NathanRobert Cummings wrote:Doesn't appear to work on the following: $input = ' 1 2 3 4 5 6'; Additionally, your solution modifies lines that weren't asked to be modified :) I realize it potentially makes for a more succinct solution (if you ever get it to work properly in the general case) but it is not a valid solution for the requested functionality-- The OP did not ask for trimming of lines with content ;)Nathan Rixham wrote:filed under 'works for me' <?php $input = 'blah b asd as d asd a sd da asd d asdNathan Rixham wrote:Hi Nathan, You may want to start testing your solutions. None have worked yet. Not even close :)Robert Cummings wrote:ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( "/(\h)\h+/im", '\\1', $input ); and vertical only: preg_replace( "/(\v)\v+/im", '\\1', $input ); (spot a pattern?)tedd wrote:preg_replace( "/(\s)\s+/im", '\\1', $input ); :)At 1:02 PM -0400 5/22/10, Robert Cummings wrote:Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :)tedd wrote: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.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.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 );da';echo preg_replace( "/(\s)\s+/im", '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here!