Normalize spaces between words in PHP

Last updated on 22/07/2016
$title = 'The Institute for Pure and            Applied Mathematics        ';
// Normalize space between words.
$patterns = array("/\s+/", "/\s([?.!])/");
$replacer = array(" ", "$1");
$title = preg_replace($patterns, $replacer, $title);

Result:

$title => The Institute for Pure and Applied Mathematics