php去掉字符串中空格的方法:1、使用php函数trim去除;2、使用php函数str_replace去除;3、使用php函数strtr去除;4、使用trimall方法去除;5、通过正则去掉普通空格等等。
php中去除字符串中的空格
1.使用php函数trim():trim($str)
可去除字符串两侧的普通空格;
2.使用php函数 :str_replace():str_replace(' ','',$str)
3.使用php函数:strtr():strtr($str,array(' '=>''))
4.使用自己写的封装函数:function trimall($str)//删除空格
{
$limit=array(" "," ","\t","\n","\r");
$rep=array("","","","","");
return str_replace($limit,$rep,$str);
}
5.使用该正则去掉普通空格:preg_replace('# #', '', $str)
6.使用该正则去掉所有空格包括全角空格:$str =preg_replace("/\s| /","",$str);