Devasish Devasish - 4 months ago 38
PHP Question

Replace \n with '' by using str_replace() in PHP

I have the following code

$s = '\n \n ';
$s = str_replace('\n', '', $s);

echo $s;

I want to replace the
char with
but it's not working with the above code. I have found that as
is the new line char with ascii value 10 by
echo ord(substr($s, 0, 1));
it is not working. It's not clear to me what is the exact reason behind not working the above code. please help.


You need to place the \n in double quotes. Inside single quotes it is treated as 2 characters '\' followed by 'n'

Try below code:

$s = "\n \n";
$s = str_replace("\n", '', $s);

echo $s;