Home > Article > Backend Development > PHP removes newline characters and uses PHP_EOL variable_PHP tutorial
A small line break actually has different implementations on different platforms. Why is this? The world is diverse. Originally, /n was used to replace line breaks in the Unix world, but in order to reflect the difference, Windows uses /r/n. What is more interesting is that /r is used in Mac. Therefore, the Unix series uses /n, the Windows series uses /r/n, and the Mac uses /r. This will cause a lot of trouble for the program you write to run on different platforms. Here are some common ways to remove newlines in PHP.
The first way of writing:
$content=str_replace("\n","",$content); echo $content;
The second way of writing:
str_replace("\r\n","",$str);
The third way of writing:
$content=preg_replace("/\s/","",$content); echo $content;
n Soft return: In Windows, it means a line break and returns to the beginning of the next line. In Linux and Unix, it only means a line break, but it does not return to the beginning of the next line.
r Soft space: In Linux and Unix, it means returning to the beginning of the current line. In Mac OS, it means breaking a line and returning to the beginning of the next line, which is equivalent to the effect of n in Windows.
t Tab (move to next column)
A few notes:
Additional code:
<?php //php 不同系统的换行 //不同系统之间换行的实现是不一样的 //linux 与unix中用 /n //MAC 用 /r //window 为了体现与linux不同 则是 /r/n //所以在不同平台上 实现方法就不一样 //php 有三种方法来解决 //1、使用str_replace 来替换换行 $str = str_replace(array("/r/n", "/r", "/n"), "", $str); //2、使用正则替换 $str = preg_replace('//s*/', '', $str); //3、使用php定义好的变量 (建议使用) $str = str_replace(PHP_EOL, '', $str); ?>
PHP_EOL is a defined variable that represents the newline character of PHP. This variable will change according to the platform. It will be /r/n under Windows, /n under Linux, and /r under Mac. .Just press the following to change the line.
$str = str_replace(PHP_EOL, '', $str);