How to remove new line character?


Please check the following text file:

As you can see, it seems to have two lines as follows:


Now, please download that text file and open it with Notepad (To download it, you can use “save page as” feature of the browser). You will see that it seems to have one line but there is an invisible new line character between 1 and 2. How can I remove this hidden character from this file using str_replace()?

I tried the following separately with no success:

$file_content = str_replace(PHP_EOL, '', $file_content);
$file_content = str_replace('\\r', '', $file_content);
$file_content = str_replace('\
', '', $website_content);

Thanks for any ideas.

trim will remove these characters

Thanks but trim doesn’t work here. That character is not at the beginning or end of the string.

Have you tried using Ascii Characters with str_replace()?

\ = chr(92)
n = chr(110)
r = chr(114)

You need to use double quotes instead of single quotes.

$file_content = str_replace(PHP_EOL, '', $file_content); 
$file_content = str_replace("\\r", '', $file_content); 
$file_content = str_replace("\
", '', $website_content); 

Single Quotes tell PHP to use exactly those characters, double quotes will parse those into return feed and line feed accordingly.

Thank you very much. I totally missed that. With double quotes it just works as I needed.