Re: how to strip empty lines out of a txt using preg_replace()
| From: | Ashley Sheridan | Date: | Fri, 04 Sep 2009 14:26:16 +0000 |
| Subject: | Re: how to strip empty lines out of a txt using preg_replace() | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297678@lists.php.net to get a copy of this message | ||
On Fri, 2009-09-04 at 16:13 +0200, Ralph Deffke wrote:
> and this is the PHP_EOL solution:
> $dtd = preg_replace( "/[". PHP_EOL . "]+/", "". PHP_EOL .
> "", $dtd);
>
> dont ask me why two empty strings are needed to surround the PHP_EOL but its
> does it.
>
> Why this works? we have got an INTERPRETER here any \n is transtlated into
> 0x0D an \r into 0x0A so the pattern does not reach prce as '\n' hehe
>
>
> "Martin Scotta" <martinscotta@gmail.com> wrote in message
>
> news:6445d94e0909040653i44716f79m972f1105559901a@mail.gmail.com...
> > On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke <ralph_deffke@yahoo.de>
> wrote:
> >
> > > the problem is some have got \t\n
> > > some are just \n\n....\n
> > >
> > > using PHP_EOL is a must
> > >
> > > I thing must be something with the /../sm attributes to the regex, spend
> > > like half an hour, but didn't get it, I'm running against a dead line,
> > > doesn't seem to be that easy if regex is not the everydays need u have
> > >
> > >
> > > "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message
> > > news:1252071327.24700.152.camel@localhost...
> > > > On Fri, 2009-09-04 at 15:28 +0200, Ralph Deffke wrote:
> > > > > ok
> > > > > preg_replace( "/^\s*$/m", "", $somestring)
> > > > > does not take empty lines out
> > > > >
> > > > > "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in
> > > > > message
> > > > >
> > > > > news:1252069539.24700.150.camel@localhost...
> > > > > > On Fri, 2009-09-04 at 14:58 +0200, Ralph Deffke wrote:
> > > > > > > Hi all, I'm a bit under stress, maybe somebody knows the
> > > > > > > regex
> on a
> > > > > snap.
> > > > > > > using PHP_EOL would be great.
> > > > > > >
> > > > > > > thanks
> > > > > > > ralph_deffke@yahoo.de
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > The regex that would match a line containing only whitespace would
> > > look
> > > > > > like this:
> > > > > >
> > > > > > ^\s*$
> > > > > >
> > > > > > Thanks,
> > > > > > Ash
> > > > > > http://www.ashleysheridan.co.uk
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > >
> > > > Are the lines actually whitespace, or are they actually <br/> tags
> that
> > > > are inserting lines to format the page for HTML display?
> > > >
> > > > Thanks,
> > > > Ash
> > > > http://www.ashleysheridan.co.uk
> > > >
> > > >
> > > >
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> > The PHP_EOL is system dependent. If you want a solution that works on
> every
> > type of file you have to code it yourself. Here you have a function made
> > some time ago.
> > Maybe you can improve it.
> >
> > If you want the result as a text format you can implode( PHP_EOL,
> $buffer )
> > Hope this helps you.
> >
> > function explode($code)
> > {
> > $lines = array();
> > $buffer = '';
> >
> > for($i=0, $len = strlen($code); $i<$len; ++$i)
> > switch( $code{$i} )
> > {
> > case "\r":
> > case "\n":
> > if( $i+1 == $len )
> > break 2;
> >
> > if( "\r" == ($next = $code{ $i+1 }) || "\n" == $next )
> > {
> > ++$i;
> > }
> >
> > $lines[] = $buffer;
> > $buffer = '';
> > break;
> > default:
> > $buffer .= $code{$i};
> > }
> >
> > if( '' !== $buffer );
> > $lines[] = $buffer;
> >
> > return $lines;
> > }
> >
> >
> > --
> > Martin Scotta
> >
>
>
>
The empty strings are forcing the parameter to be used as a string,
otherwise the line would be translated as something like this:
$dtd = preg_replace( "/\n/", \n, $dtd);
Notice how that newline character is actually just the character by
itself, and is not a string.
Obviously, the exact end of line character will differ from system to
system, but this is a simple example of exactly why you had to
concatenate the PHP_EOL with those empty strings.
Thanks,
Ash
http://www.ashleysheridan.co.uk