删除多个空格

时间:2010-05-22 05:07:51

标签: php removing-whitespace

所以,如果我有一个像

这样的字符串
"hello    what is  my    name"

如何获取所有空格并仅用一个空格替换每个空格?

3 个答案:

答案 0 :(得分:32)

这应该这样做:

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

<强>输出:

hello what is my name

答案 1 :(得分:3)

找到解决方案:

<?php

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

?>

答案 2 :(得分:0)

尝试以下操作:-

$desc = "hello    what is  my    name";
echo trim(preg_replace('/\s+/', ' ', $desc));
相关问题