A php variable contains the following string:
text text2 item1 item2
text
text2
You need to place the \n in double quotes. Inside single quotes it is treated as 2 characters '\' followed by 'n'
\n
'\'
'n'
You need:
$str = str_replace("\n", '', $str);
A better alternative is to use PHP_EOL as:
PHP_EOL
$str = str_replace(PHP_EOL, '', $str);