php method to remove newlines in strings: You can use the php system constant PHP_EOL combined with the str_replace() function to achieve this. Line breaks in PHP can be replaced with PHP_EOL. The str_replace() function is used to replace some characters in a string.
str_replace() function replaces some characters in a string (case-sensitive) and returns a string or array with the replacement value.
(Recommended tutorial: php graphic tutorial)
Grammar:
str_replace(find,replace,string,count)
(Recommended learning video: php video tutorial )
Implementation code:
// 1)使用转义字符函数 <?php $str = str_replace(array("/r/n", "/r", "/n"), '', $str); ?> // 2)使用正则表达式替换 <?php $str = preg_replace('//s*/', '', $str); ?> // 3)使用PHP系统常量【推荐】 $str = str_replace(PHP_EOL, '', $str); ?>
The above is the detailed content of How to remove newline characters from string in php. For more information, please follow other related articles on the PHP Chinese website!