Php method for removing line breaks (use of the PHP_EOL variable) _ PHP Tutorial

Source: Internet
Author: User
Php method for removing line breaks (use of the PHP_EOL variable ). A small line feed has different implementations on different platforms. why is this possible? the world is diverse. Originally, the line feed in the unix world was replaced by n. However, a small line feed in the window actually has different implementations on different platforms. why is this possible? the world is diverse. Originally, the line feed in the unix world was replaced by/n, but in order to reflect its differences, windows uses/r/n. What's more interesting is that/r is used in mac. Therefore, unix/n, windows/r/n, and mac/r are used to run programs on different platforms. Below are some common methods for removing line breaks in PHP.

Method 1:

The code is as follows:

$ Content = str_replace ("\ n", "", $ content );
Echo $ content;



Method 2:

The code is as follows:

Str_replace ("\ r \ n", "", $ str );

Method 3:

The code is as follows:


$ Content = preg_replace ("/\ s/", "", $ content );
Echo $ content;

About \ n, \ r, \ t
\ N soft press enter: in Windows, it indicates a line break and returns to the start position of the next line. in Linux and unix, it only indicates a line break, but does not return to the start position of the next line.
\ R soft space: in Linux and unix, it indicates to return to the initial position of the row. In Mac OS, line breaks are displayed and returned to the start position of the next line, which is equivalent to \ n in Windows.
\ T hop (move to the next column)

Notes:

They are valid in double quotes or delimiters, and are invalid in single quotes.
\ R \ n is generally used together to represent the carriage return key (in Linux and Unix) on the keyboard. \ n (in Windwos) can also be used only, and \ r is used to represent the carriage return in Mac OS.
\ T indicates the "TAB" key on the keyboard.
File line break: windows: \ n, linux, unix: \ r \ n

Additional code:

The code is as follows:


// Line feed for different php systems
// The implementation of line feed varies with systems.
// In linux and unix/n
// For MAC/r
// Window is/r/n to reflect differences from linux
// The Implementation methods on different platforms are different.
// Php has three solutions

// 1. use str_replace to replace the line feed.
$ Str = str_replace (array ("/r/n", "/r", "/n"), "", $ str );

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

// 3. use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );
?>


PHP_EOL is a defined variable that represents the php line break. this variable will change according to the platform. in windows, it will be/r/n, and in linux it will be/n, under mac is/r. line Feed is followed by the following line.

The code is as follows:

$ Str = str_replace (PHP_EOL, '', $ str );

Bytes. In the unix world, the newline is replaced by/n, but window...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.