I don't think it's a problem with the specific code you're using to remove newlines. Rather, the problem is that for some reason you have actual back-slashes followed by "n" characters in the text, not newline characters. (It's as if the text were defined as $text = 'first line\\nsecond line'; rather than $text = "first line\nsecond line";)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation