Remove Unnecessary Spaces Function with PHP

If you want to reduce multiple spaces at the beginning, end and middle of string values to a single space, you can use this function. In short, it deletes all unnecessary spaces.

<?php

function replaceSpace($string)
{
	$string = preg_replace("/\s+/", " ", $string);
	$string = trim($string);
	return $string;
}

$string = '     test             test             ';
print replaceSpace($string);
Comments

There are no comments, make the firs comment