Php 用str_replace删除多余的空白

Php 用str_replace删除多余的空白,php,arrays,string,str-replace,space,Php,Arrays,String,Str Replace,Space,标题和代码几乎是不言自明的 但为了进一步澄清 我想在$myString中的每个单词之间留一个空格(并删除不好的单词) 如果可能的话,我宁愿呆在单行线上 $myString = 'There will be no extra space here !!!'; str_replace(array("bad words","another bad words","\s+"), '', $myString); 我希望得到: There will be n

标题和代码几乎是不言自明的

但为了进一步澄清

我想在
$myString
中的每个单词之间留一个空格(并删除不好的单词)

如果可能的话,我宁愿呆在单行线上

$myString = 'There    will      be     no   extra    space       here !!!';

str_replace(array("bad words","another bad words","\s+"), '', $myString);
我希望得到:

There will be no extra space here !!!
There will be no extra space here !!!
谢谢

试试这个

$myString =     preg_replace('/\s\s*/', ' ',$myString);
试试这个

$myString =     preg_replace('/\s\s*/', ' ',$myString);
查看演示
str\u replace
替换字符串的特定匹配项。在您的情况下,您只需删除不需要替换的空白,因此
preg\u replace
最适合您的情况

要删除所有不需要的空白,只需这样做

代码 查看演示
str\u replace
替换字符串的特定匹配项。在您的情况下,您只需删除不需要替换的空白,因此
preg\u replace
最适合您的情况

要删除所有不需要的空白,只需这样做

代码 我想你想要:

$myString = 'There    will      be     no   extra    space       here !!!';

str_replace(array("bad words","another bad words","\s\s"), array("","", " "), $myString);
您可以在$replace参数中使用数组,该数组中的索引元素与$search参数中的元素相对应

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
我想你想要:

$myString = 'There    will      be     no   extra    space       here !!!';

str_replace(array("bad words","another bad words","\s\s"), array("","", " "), $myString);
您可以在$replace参数中使用数组,该数组中的索引元素与$search参数中的元素相对应

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

是的,我知道这个技巧,但我想知道是否有可能在内部实现:str_replace(数组(“坏单词”,“另一个坏单词”,“\s+{2}”),“$myString”);是的,我知道这个技巧,但我想知道是否有可能在内部实现:str_replace(数组(“坏单词”,“另一个坏单词”,“\s+{2}”),“$myString”);是的,我知道这个诀窍,但我想知道是否有可能在str_replace内完成(不使用另一行)…是的,我知道那个诀窍,但我想知道是否有可能在str_replace内完成(不使用另一行)…是的,我知道那个诀窍,但我想知道是否有可能在str_replace内完成(不使用其他额外的行)…是的,我知道这个技巧,但我想知道是否有可能在str_replace(不使用其他额外的行)…内完成它。。。