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

2009-09-04 Thread 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_def...@yahoo.de



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



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

2009-09-04 Thread 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_def...@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: [PHP] how to strip empty lines out of a txt using preg_replace()

2009-09-04 Thread Ralph Deffke
ok
 preg_replace( /^\s*$/m, , $somestring)
does not take empty lines out

Ashley Sheridan a...@ashleysheridan.co.uk wrote in message
news:1252069539.24700.150.ca...@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_def...@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: [PHP] how to strip empty lines out of a txt using preg_replace()

2009-09-04 Thread 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 a...@ashleysheridan.co.uk wrote in message
 news:1252069539.24700.150.ca...@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_def...@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



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

2009-09-04 Thread 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 a...@ashleysheridan.co.uk wrote in message
news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
  news:1252069539.24700.150.ca...@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_def...@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



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

2009-09-04 Thread 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 a...@ashleysheridan.co.uk wrote in message
 news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
   news:1252069539.24700.150.ca...@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_def...@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
 
 
 
 
 
 
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: [PHP] how to strip empty lines out of a txt using preg_replace()

2009-09-04 Thread Ralph Deffke
I'm working on DTD's

Ashley Sheridan a...@ashleysheridan.co.uk wrote in message
news:1252071932.24700.153.ca...@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 a...@ashleysheridan.co.uk wrote in message
  news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
news:1252069539.24700.150.ca...@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_def...@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
  
  
  
 
 
 
 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: [PHP] how to strip empty lines out of a txt using preg_replace()

2009-09-04 Thread Ashley Sheridan
On Fri, 2009-09-04 at 15:45 +0200, Ralph Deffke wrote:
 I'm working on DTD's
 
 Ashley Sheridan a...@ashleysheridan.co.uk wrote in message
 news:1252071932.24700.153.ca...@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 a...@ashleysheridan.co.uk wrote in message
   news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
 news:1252069539.24700.150.ca...@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_def...@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
   
   
   
  
  
  
  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: [PHP] how to strip empty lines out of a txt using preg_replace()

2009-09-04 Thread Martin Scotta
On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke ralph_def...@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 a...@ashleysheridan.co.uk wrote in message
 news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
   news:1252069539.24700.150.ca...@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_def...@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


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

2009-09-04 Thread Ralph Deffke
this works
$dtd = preg_replace( /\n+/, \n, $dtd);

Martin Scotta martinsco...@gmail.com wrote in message
news:6445d94e0909040653i44716f79m972f11055599...@mail.gmail.com...
 On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke ralph_def...@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 a...@ashleysheridan.co.uk wrote in message
  news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
news:1252069539.24700.150.ca...@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_def...@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




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



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

2009-09-04 Thread Sam Stelfox
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 a...@ashleysheridan.co.uk wrote in message
news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
news:1252069539.24700.150.ca...@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_def...@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








  




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

2009-09-04 Thread 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 martinsco...@gmail.com wrote in message
news:6445d94e0909040653i44716f79m972f11055599...@mail.gmail.com...
 On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke ralph_def...@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 a...@ashleysheridan.co.uk wrote in message
  news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
news:1252069539.24700.150.ca...@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_def...@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




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



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

2009-09-04 Thread 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 martinsco...@gmail.com wrote in message
 news:6445d94e0909040653i44716f79m972f11055599...@mail.gmail.com...
  On Fri, Sep 4, 2009 at 10:37 AM, Ralph Deffke ralph_def...@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 a...@ashleysheridan.co.uk wrote in message
   news:1252071327.24700.152.ca...@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 a...@ashleysheridan.co.uk wrote in message
 news:1252069539.24700.150.ca...@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_def...@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




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



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

2009-09-04 Thread Jim Lucas
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