Removing more than one white-space

ThinkingInBits picture ThinkingInBits · May 22, 2010 · Viewed 17.1k times · Source

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?

Answer

Web Logic picture Web Logic · May 22, 2010

This should do it:

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

Output:

hello what is my name