How to delete newline characters in php: 1. Use "str_replace(PHP_EOL, '', string)" to delete. "PHP_EOL" is a newline character variable defined in PHP; 2. Use "preg_replace ('//s*/', '', string)" to delete.
Recommended: "PHP Video Tutorial"
php Line breaks in different systems, and line breaks between different systems The implementation is different:
Used in linux and unix\n
Used in MAC\r
window In order to reflect the difference from Linux\r\n
So the implementation methods are different on different platforms. PHP has three ways to solve it
Method 1: Use str_replace to replace newlines
$str = str_replace(array("\r\n", "\r", "\n"), "", $str);
The str_replace() function replaces some characters in a string (case sensitive).
Method 2: Using regular replacement
$str = preg_replace('//s*/', '', $str);
preg_replace function performs a regular expression search and replacement.
Method 3: Use variables defined by php (recommended)
$str = str_replace(PHP_EOL, '', $str);
PHP_EOL is a defined variable that represents the newline character of php. This variable will It varies according to the platform. Under Windows, it will be /r/n, under Linux, it will be /n, and under Mac, it will be /r. Just press the following to change the line.
Line breaks in PHP can be replaced by PHP_EOL, which can improve the source code level portability of the code
Related recommendations: php training
The above is the detailed content of How to remove newline characters in php?. For more information, please follow other related articles on the PHP Chinese website!