PHP中替换换行符
<?php
$str="this is a test \n";
$patten = array("\r\n", "\n", "\r");
//先替换掉\r\n,然后是否存在\n,最后替换\r $str=str_replace($order, "", $str); ?>
//php 有三种方法来解决
//1、使用str_replace 来替换换行
$str = str_replace(array("\r\n", "\r", "\n"), "", $str);
//2、使用正则替换
$str = preg_replace(\'//s*/\', \'\', $str);
//3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, \'\', $str);
/*
* 获得用户操作系统的换行符,\n
* @access public
* @return string
*/
function get_crlf()
{
if (stristr($_SERVER[\'HTTP_USER_AGENT\'], \'Win\'))
{
$the_crlf = \'\r\n\';
}
elseif (stristr($_SERVER[\'HTTP_USER_AGENT\'], \'Mac\'))
{
$the_crlf = \'\r\'; // for old MAC OS
}
else
{
$the_crlf = \'\n\';//权重大一点
}
return $the_crlf;
}
注意:在前台页面显示的时候,用nl2br使换行变成<br>