php去掉字符串中的空格

php去掉字符串中空格的方法:1、使用php函数trim去除;2、使用php函数str_replace去除;3、使用php函数strtr去除;4、使用trimall方法去除;5、通过正则去掉普通空格等等。

推荐:《PHP视频教程

php中去除字符串中的空格

1.使用php函数trim():

1

trim($str)

可去除字符串两侧的普通空格;

2.使用php函数 :str_replace():

1

str_replace(' ','',$str)

3.使用php函数:strtr():

 

1

strtr($str,array(' '=>''))

4.使用自己写的封装函数:

 

1

2

3

4

5

6

function trimall($str)//删除空格

{

    $limit=array(" "," ","\t","\n","\r");

    $rep=array("","","","","");

    return str_replace($limit,$rep,$str);

}

5.使用该正则去掉普通空格:

1

preg_replace('# #', '', $str)

6.使用该正则去掉所有空格包括全角空格:

1

$str =preg_replace("/\s| /","",$str);

以上就是php去掉字符串中的空格的详细内容

未经允许不得转载:肥猫博客 » php去掉字符串中的空格

赞 (0)
分享到: 更多

评论 0