所以,如果我有一个字符串像 这样的话
"hello what is my name"
我该如何将所有空格取代为一个空格?
"hello what is my name"
我该如何将所有空格取代为一个空格?
这应该可以解决问题:
$replaced = preg_replace('/\s\s+/', ' ', $text);
输出:
hello what is my name
找到了解决方案:
<?php
$str = ' This is a test ';
$count = 1;
while($count)
$str = str_replace(' ', ' ', $str, $count);
?>
试试这个:
$desc = "hello what is my name";
echo trim(preg_replace('/\s+/', ' ', $desc));