Re: Remove blank lines from a file

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

 



Robert Cummings wrote:
Nathan Rixham wrote:
Robert Cummings wrote:
You may want to start testing your solutions. None have worked yet. Not even close :)
filed under 'works for me'

<?php
$input = 'blah b  asd as d
asd
a
sd

da




  asd
   d
   asd


    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!
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 ;)

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)

I think I see an issue... either my client, your client, both, or the PHP list is trimming email lines. I have spaces at the end of some of the lines in my example. Let's see if a text file attachment works for this.

FYI my command-line is currently running 5.2.11

Yes it was client stripping out extra whitespace! thanks Rob, replicated your results:
1
2 3
4
 5 6

and then 'fixed' to give what's needed:
  preg_replace( "/(((\r|)\n)(\h*|))+/im", '\\1' ,  $input );

the above keeps line termination the same as in the source file; can you give it a quick check your side to ensure it works (if you don't mind)?

modified one that 'cleans' the new lines:
 preg_replace( "/((\r|)\n(\h*|))+/im", PHP_EOL ,  $input );

Best,

Nathan

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