This article mainly introduces the example of removing string line breaks in php. The following describes common methods for removing line breaks. For more information, see the following 1st methods:
The code is as follows:
Str_replace ("n", '', $ str );
?>
2nd writing methods:
The code is as follows:
Str_replace ("rn", '', $ str );
?>
3rd writing methods:
The code is as follows:
Preg_replace ("/s/", '', $ str );
?>
The following is a description:
First, let's talk about n, r, t
N soft carriage return:
In Windows, line breaks are displayed and returned to the beginning of the next line.
In Linux/unix, it only indicates line breaks, but does not return to the starting position of the next line.
R soft space:
In Linux/unix, return to the starting position of the row
In Mac OS, line breaks are represented and returned to the start position of the next line, which is equivalent to n in Windows.
T-hop (move to the next column)
Note:
They are valid in double quotes or delimiters, and are invalid in single quotes.
Rn is generally used together to represent the enter key on the keyboard (in Linux and Unix). You can also use n (in Windwos) and press enter in Mac OS!
T indicates the TAB key on the keyboard.
Line Feed characters in the file:
Windows: n
Linux/unix: rn
The following code demonstrates three common methods for removing line breaks from strings in PHP:
1. use escape character functions
The code is as follows:
$ Str = str_replace (array ("/r/n", "/r", "/n"), '', $ str );
?>
2. replace with a regular expression
The code is as follows:
$ Str = preg_replace ('// s */', '', $ str );
?>
3. PHP system constants are recommended.
The code is as follows:
$ Str = str_replace (PHP_EOL, '', $ str );
?>