Removing more than one white-space

2019-04-20 17:41发布

问题:

So, If I have a string like

"hello    what is  my    name"

How can I take all of the spaces and replace each with only one space?

回答1:

This should do it:

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

Output:

hello what is my name


回答2:

Found the solution:

<?php

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

?>