How to remove whilespace from string in php?

To remove multiple occurences of whitespace characters in a string an convert them all into single spaces in php.

1. Code:

     <?php 
	function removeUnnecessarySpaces($str){
		return trim(preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str));
	}
	$str = "Soltuts.com       is   providing      PHP tutorials,      tips,     tricks      and code snippets";
	$st = removeUnnecessarySpaces($str);
	echo($st);
    ?> 

2. Example:

Input:

"Soltuts.com       is   providing      PHP tutorials,      tips,     tricks      and code snippets"

Output:

Soltuts.com is providing PHP tutorials, tips, tricks and code snippets