Summary of how to remove line breaks in php (use of PHP_EOL variable)_PHP tutorial

WBOY
Release: 2016-07-21 15:12:45
Original
825 people have browsed it

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:

Copy code The code is as follows:
$content=str_replace("n","",$content);
echo $ content;


Second way of writing:

Copy code The code is as follows:
str_replace("rn","",$str);

The third way of writing:

Copy code The code is as follows:

$content=preg_replace("/s/","",$content);
echo $content;

About n, r, t
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. Location.
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:

They are valid in strings represented by double quotes or delimiters, but not in strings represented by single quotes.
rn are generally used together to represent the Enter key on the keyboard (in Linux and Unix). You can also just use n (in Windows). In Mac OS, use r to represent Enter.
t represents the "TAB" key on the keyboard.
Newline symbol in file: windows: n, linux, unix: rn

Supplementary code:

Copy code The code is as follows:

//php for different systems Line breaks
//The implementation of line breaks is different between different systems
///n is used in linux and unix
///r is used in MAC
//window to reflect the difference from linux It’s /r/n
//So the implementation methods are different on different platforms
//php has three ways to solve it

//1. Use str_replace to replace newlines
$str = str_replace(array("/r/n", "/r", "/n"), "", $str);

//2. Use regular replacement
$str = preg_replace('//s*/', '', $str);

///3. Use variables defined by PHP (recommended)
$str = str_replace(PHP_EOL, '', $str);
?>


PHP_EOL It is a defined variable that represents the newline character of PHP. This variable will change according to the platform. Under Windows, it will be /r/n, under Linux, it will be /n, and under Mac, it will be /r. To break a new line, press The following will do.
Copy code The code is as follows:
$str = str_replace(PHP_EOL, '', $str);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326612.htmlTechArticleA small line break, in fact, there are different implementations on different platforms. Why is this? The world can be Diverse. Originally, /n was used to replace line breaks in the unix world, but window...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!