所以,如果我有一个像
这样的字符串"hello what is my name"
如何获取所有空格并仅用一个空格替换每个空格?
答案 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));