方法:1、用“str_replace(array("\r\n","\n"),'',变量)”语句;2、用“preg_replace('/[\r\n]/','',变量)”语句;3、用“str_replace(PHP_EOL,'',变量)”语句。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
php替换换行符的方法:
$str="this is a test \n";
方法一:
$replace_str = str_replace(array("\r\n", "\r", "\n"), "", $str);
(注意:先替换掉\r\n,然后是否存在\n,最后替换\r)
方法二:
$replace_str = preg_replace('/[\r\n]/', '', $str);
(原文写的是/\s*/,但是这样会把内容里的空白字符也替换掉)
方法三:
$replace_str = str_replace(PHP_EOL, '', $str);
推荐学习:《PHP视频教程》
以上就是php怎么将换行符替换掉的详细内容,更多请关注php中文网其它相关文章!
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
- 上一篇:php怎么关闭警告信息
- 下一篇:php怎么把字符串转为utf-8编码
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论