views:

63

answers:

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.

+1  A: 

str_replace can be passed an array as:

$line_out = str_replace(array("\r","\n"), '', $line_in);
codaddict
THANK YOU!! Hopefully I will be able to figure this stuff out a little better soon.
Stephayne
+2  A: 

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:

Lekensteyn
Your answer took care of another question and answered my first as well. Thank you!
Stephayne
A: 

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