发布日期:2018-03-26
在PHP中怎么去除字符串中所有的空格+ 查看更多
在PHP中怎么去除字符串中所有的空格
+ 查看更多
发布日期:2017-04-08 15:26
分类:PHP
浏览次数:161
例如有如下的字符串
$string = "this is my string";
怎么把它转化为下面的样子?
"thisismystring"
回答
如果只是移除空格,可以使用 str_replace:
$string = str_replace(' ', '', $string);
如果是所有的空格,可以使用preg_replace:
$string = preg_replace('/\s+/', '', $string);