2010-05-22 74 views

回答

28

这应做到:

$replaced = preg_replace('/\s\s+/', ' ', $text); 

输出:

hello what is my name 
+0

很聪明.............. – Umair 2016-11-19 16:51:03

3

找到了解决办法:

<?php 

$str = ' This is a test '; 
$count = 1; 
while($count) 
    $str = str_replace(' ', ' ', $str, $count); 

?> 
+5

这不是真的是最完美的解决方案......逸岸这是相当效率低下。 – 2010-05-22 05:21:23

+0

对于不熟悉正则表达式的人来说,这是一个很好的解决方案。 “效率”在这里并不重要。 – 2010-05-22 05:33:05

+3

哇,回顾一下多年前我被吸引的程度会更糟。 – ThinkingInBits 2013-10-15 18:32:36