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
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
1st writing 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 );
?>