例如: $val = "hello \nworld"; 这里是存入数据库的值 想在页面上显示为
hello
world
1、 php 代码 nl2br($val);
2、php str_replace
3、在模板HTML上下功夫
借助<pre></pre> 标签来处理自动换行
但是如果内容的长度太长,保存的时候没有换行在显示的时候也会超出长度。
解决方法: css 来控制
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
这样就彻底解决啦。
1.// 第1种写法:
str_replace("\n", '', $str);
?>
// 第2种写法:
str_replace("\r\n", '', $str);
?>
// 第3种写法:
preg_replace("/\s/", '', $str);
?>
下面以代码说明PHP中去除字符串中换行的三种常用的方法
//使用转义字符函数
$str = str_replace(array("/r/n", "/r", "/n"), '', $str);
?>
// 2)使用正则表达式替换
$str = preg_replace('//s*/', '', $str);
?>
// 3)使用PHP系统常量【推荐】
$str = str_replace(PHP_EOL, '', $str);
?>

下一篇:Linux 解压 .rar后缀的压缩文件