3
$line-out = str_replace('\r', '', str_replace('\n', '', $line-in));

The above works for me but, I saw a [\n\r] example somewhere and I cannot seem to find it.

I just want to get rid any blank lines. The above is in a foreach loop.

Thanks for teaching.

Stephayne
  • 73
  • 1
  • 1
  • 3
  • you are likely looking for this one [Remove new lines from string](http://stackoverflow.com/questions/3760816/remove-new-lines-from-string) – Gordon Sep 23 '10 at 14:41

3 Answers3

10

You shouldn't use - in variable names ;)

$line_out = preg_replace('/[\n\r]+/', '', $line_in);
$line_out = str_replace(array("\n", "\r"), '', $line_in);

Manual entries:

William
  • 899
  • 12
  • 34
Lekensteyn
  • 61,566
  • 21
  • 152
  • 187
3

str_replace can be passed an array as:

$line_out = str_replace(array("\r","\n"), '', $line_in);
codaddict
  • 429,241
  • 80
  • 483
  • 523
0

This is from php.net's example #2 in str_replace (modified to suit the "environment"):

<?php
// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, '', $str);
nush
  • 11
  • 1
  • 2