how to strip empty lines out of a txt using preg_replace()

how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 14:58:55 von Ralph Deffke

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



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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:05:39 von Ashley Sheridan

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




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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:28:52 von Ralph Deffke

ok
preg_replace( "/^\s*$/m", "", $somestring)
does not take empty lines out

"Ashley Sheridan" 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
>
>
>



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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:35:27 von Ashley Sheridan

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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:37:05 von Ralph Deffke

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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:45:01 von Ralph Deffke

I'm working on DTD's

"Ashley Sheridan" wrote in message
news:1252071932.24700.153.camel@localhost...
> On Fri, 2009-09-04 at 15:37 +0200, Ralph Deffke 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" 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" 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
tags
that
> > > are inserting lines to format the page for HTML display?
> > >
> > > Thanks,
> > > Ash
> > > http://www.ashleysheridan.co.uk
> > >
> > >
> > >
> >
> >
> >
> If it is just made up of \t and \n then \s in the regex should match it,
> as it's meant to match just whitespace characters. Where are you getting
> the content from anyway?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>



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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:45:32 von Ashley Sheridan

On Fri, 2009-09-04 at 15:37 +0200, Ralph Deffke 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" 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" 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
tags that
> > are inserting lines to format the page for HTML display?
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
>
>
>
If it is just made up of \t and \n then \s in the regex should match it,
as it's meant to match just whitespace characters. Where are you getting
the content from anyway?

Thanks,
Ash
http://www.ashleysheridan.co.uk




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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:48:49 von Ashley Sheridan

On Fri, 2009-09-04 at 15:45 +0200, Ralph Deffke wrote:
> I'm working on DTD's
>
> "Ashley Sheridan" wrote in message
> news:1252071932.24700.153.camel@localhost...
> > On Fri, 2009-09-04 at 15:37 +0200, Ralph Deffke 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" 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" 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
tags
> that
> > > > are inserting lines to format the page for HTML display?
> > > >
> > > > Thanks,
> > > > Ash
> > > > http://www.ashleysheridan.co.uk
> > > >
> > > >
> > > >
> > >
> > >
> > >
> > If it is just made up of \t and \n then \s in the regex should match it,
> > as it's meant to match just whitespace characters. Where are you getting
> > the content from anyway?
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
>
>
>
OK, but where are they coming from? Are they as external files or what?

Thanks,
Ash
http://www.ashleysheridan.co.uk




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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 15:53:31 von Martin Scotta

--0016363b8e1c4e3ac30472c0d39d
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke 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" 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" 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
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

--0016363b8e1c4e3ac30472c0d39d--

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 16:05:38 von Ralph Deffke

this works
$dtd = preg_replace( "/\n+/", "\n", $dtd);

"Martin Scotta" wrote in message
news:6445d94e0909040653i44716f79m972f1105559901a@mail.gmail. com...
> On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke
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" 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" 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
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
>



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

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 16:08:06 von Sam Stelfox

--------------020205070502050309010600
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

The following snippet is untested and using Ash's regex (it is accurate
\s matches any white space). $content is what is getting stripped of the
new lines and $filtered is the cleansed output. See if that does the
trick for you.

$lines = str_split(PHP_EOL, $content);
$filtered = '';
foreach ($lines as $line) {
if (!preg_match('^\s*$', $line)) {
// Splitting on the PHP_EOL characters cause it to be removed be
sure to put it back
$filtered .= $line . PHP_EOL;
}
}

Ralph Deffke 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" 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" 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
tags that
>> are inserting lines to format the page for HTML display?
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>>
>
>
>
>


--------------020205070502050309010600--

Re: how to strip empty lines out of a txt using preg_replace()

am 04.09.2009 16:13:14 von Ralph Deffke

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" wrote in message
news:6445d94e0909040653i44716f79m972f1105559901a@mail.gmail. com...
> On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke
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" 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" 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
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
>



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

Re: how to strip empty lines out of a txt usingpreg_replace()

am 04.09.2009 16:26:16 von Ashley Sheridan

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" wrote in message
> news:6445d94e0909040653i44716f79m972f1105559901a@mail.gmail. com...
> > On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke
> 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" 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" 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
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




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

Re: how to strip empty lines out of a txt using preg_replace()

am 05.09.2009 00:02:18 von List Manager

Sam Stelfox wrote:
> The following snippet is untested and using Ash's regex (it is accurate
> \s matches any white space). $content is what is getting stripped of the
> new lines and $filtered is the cleansed output. See if that does the
> trick for you.
>
> $lines = str_split(PHP_EOL, $content);
> $filtered = '';
> foreach ($lines as $line) {
> if (!preg_match('^\s*$', $line)) {
> // Splitting on the PHP_EOL characters cause it to be removed be
> sure to put it back
> $filtered .= $line . PHP_EOL;
> }
> }
>

That isn't going to work. You missed your delimiters in the first arg
for preg_match(). It requires beginning and ending delimiters around
the regex.

I would try this

$lines = preg_split('|[\n\r]|', $content);
$filtered = '';
foreach ( $lines as $line ) {
$txt = trim( $line );
if ( ! empty( $txt ) ) {
$filtered .= $line;
}
}

It is much more intense to do the preg_match inside a loop. You should
only do it once, then use less intense function calls inside the loop.

warning: completely untested... have no example input you are using...


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

Re: how to strip empty lines out of a txt using preg_replace()

am 05.09.2009 04:45:02 von Clancy

On Fri, 4 Sep 2009 14:58:55 +0200, ralph_deffke@yahoo.de ("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.

Here is a function which works for files pasted from browser screens:

function clean_up($dirty_file)
{
$contents = file_get_contents ($dirty_file);
if ($contents)
{
$c_array = explode ("\n", $contents);
$n = count($c_array);
$i = 0; $j = 0; while ($i < $n)
{
$line = trim($c_array[$i]);
if ($line != '')
{
$d_array[$j++] = $c_array[$i];
}
++$i;
}
$clean = implode ("\n",$d_array);
$data_file = file_put_contents($data_file, $clean);
}
}

The virtue of this is you don't have to know, or care, what characters are on the blank
lines.


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