I replace all spaces with hyphens in my script like this:
$final = str_replace(\' \',\'-\',$final); The - is the only thing I replace.
What
Use a regular expression changing multiple spaces or hyphens with one hyphen:
$final = preg_replace('#[ -]+#', '-', $text);