Home > Article > Backend Development > PHP regular expression to filter spaces, newlines, and carriage returns_PHP Tutorial
一,下例可以去除额外空白
代码如下 | |||||
$str = " This line containstliberal rn use of whitespace.nn"; {$str}"; ?>
|
二,替换换行符
//php 有三种方法来解决
代码如下 | |||||
//1、使用str_replace 来替换换行
//2、使用正则替换 //3、使用php定义好的变量 (建议使用) |
The code is as follows:
代码如下 | |
/* * 获得用户操作系统的换行符,n * @access public * @return string */ function get_crlf() { if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win')) { $the_crlf = 'rn'; } elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac')) { $the_crlf = 'r'; // for old MAC OS } else { $the_crlf = 'n';//权重大一点 www.111cn.net } return $the_crlf; } |
Note: When displaying on the front page, use nl2br to change the line break to
Three, replace the carriage return
The code is as follows | |||||
//php line breaks in different systems
|