输入banner图图片脚本导航/分类

php怎么将换行符替换掉

方法: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怎么将换行符替换掉的详细内容,更多请关注gxlcms其它相关文章!