开发者

Removing more than one white-space

开发者 https://www.devze.com 2023-01-01 03:52 出处:网络
So, If I have a string like \"hellowhat ismyname\" How can I take all of the spaces and re开发者_Python百科place each with only one space?This should do it:

So, If I have a string like

"hello    what is  my    name"

How can I take all of the spaces and re开发者_Python百科place each with only one space?


This should do it:

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

Output:

hello what is my name


Found the solution:

<?php

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

?>


Try this:-

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

精彩评论

暂无评论...
验证码 换一张
取 消