Method: 1. Use the "str_replace(array("\r\n","\n"),'', variable)" statement; 2. Use "preg_replace('/[\r\n ]/','', variable)" statement; 3. Use the "str_replace(PHP_EOL,'', variable)" statement.
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer
How to replace line breaks in php:
$str="this is a test \n";
Method 1:
$replace_str = str_replace(array("\r\n", "\r", "\n"), "", $str);
(Note: replace \r\n first, then whether it exists\n, and finally replace \r)
Method 2 :
$replace_str = preg_replace('/[\r\n]/', '', $str);
(The original text is /\s*/, but this will also replace the blank characters in the content)
Method 3:
$replace_str = str_replace(PHP_EOL, '', $str);
Recommended learning : "PHP Video Tutorial"
The above is the detailed content of How to replace newline characters in php. For more information, please follow other related articles on the PHP Chinese website!