IT & Programming

Remove extra spaces from a string

Function accepts string as parameter a removes all repeated or duplicate spaces, newlines, tabs with a single space.

Function

function removeSpaces($text, $replace = ' ') {

	return preg_replace('!\s+!', $replace, $text);
}

Calling The Function

$string='The   life is    beautiful';

Output

The life is beautiful

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *