简体   繁体   中英

Removing more than one white-space

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?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM