小编典典

如何在PHP中从字符串中去除所有空格?

php

我怎么能 剥夺 / 删除 所有 空格 一个的 字符串 在PHP?

我有一个 字符串$string = "this is my string";

输出应为 "thisismystring"

我怎样才能做到这一点?


阅读 449

收藏
2020-05-26

共1个答案

小编典典

您是指空格还是所有空格?

对于仅空格,请使用str_replace:

$string = str_replace(' ', '', $string);

对于所有空格(包括制表符和行尾),请使用preg_replace:

$string = preg_replace('/\s+/', '', $string);
2020-05-26