Remove line feed solution summary in PHP (PHP_EOL)

  • 2020-05-10 17:49:06
  • OfStack

The first way to write:
$content=str_replace("\n","",$content);
echo $content;

The second way:
str_replace("\r\n","",$str);

The third way:
$content=preg_replace("/\s/","",$content);
echo $content;

The attached:

First,\ n,\r,\t
\n soft return:
In Windows represents a newline and returns to the beginning of the next line
In Linux, unix only means a newline, but does not return to the beginning of the next line.
\r soft space:
In Linux, unix represents a return to the beginning of the row in question.
In Mac OS represents a newline and returns to the beginning of the next line, equivalent to the effect of \n in Windows.
\t skip (move to the next column)
Some notes:
They are valid in strings represented by double quotation marks or delimiters, and not in strings represented by single quotation marks.
\r\n 1 for keyboard return key (Linux,Unix), or \n(Windwos), \r for Mac OS!
\t represents the "TAB" key on the keyboard.
Newline symbol in the file:
windows : \n
linux,unix: \r\n
Example code:
 
<?php 
//php  Line feeds for different systems  
// The implementation of line feeds between different systems is not 1 The sample of  
//linux  with unix using  /n 
//MAC  with  /r 
//window  In order to embody linux different   It is  /r/n 
// So on different platforms   The implementation method is not 1 sample  
//php  There are 3 Here's a way to do it  

//1 , the use of str_replace  So let's replace the newline  
$str = str_replace(array("/r/n", "/r", "/n"), "", $str); 

//2 , using regular substitution  
$str = preg_replace('//s*/', '', $str); 

//3 , the use of php Defined variable   (recommended)  
$str = str_replace(PHP_EOL, '', $str); 
?> 

Related articles: