php几种常见方法 去除空格和换行符
php去除空格和换行
\r是回车,英文是Carriage return,作用:使光标到行首
\n是换行,英文是New line/line feed,作用:使光标下移一行
$qian=array(" "," ","\t","\n","\r");
return str_replace($qian, '', $str);
}
php 有三种方法来解决 替换换行符
1、使用str_replace 来替换换行
$str = str_replace(array("rn", "r", "n"), "", $str);$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
2、使用正则替换
$str = preg_replace('//s*/', '', $str);
3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, '', $str);
gsgsdgs gsdg gsd';
echo myTrim($str);
function myTrim($str)
{
$search = array(" "," ","\n","\r","\t");
$replace = array("","","","","");
return str_replace($search, $replace, $str);
}
PHP去除所有的空格 去除两边的空格
trim($arr)
正则匹配去除所有的空格
preg_replace('# #','',$goodid)
去掉两个空格以上
$str = preg_replace(’/\s(?=\s)/’, ‘’, $str);