function clean($text)
{
    return trim(preg_replace("/(\s*[\r\n]+\s*|\s+)/", ' ', $text));
}

the first part \s[\r\n]+\s will replace any line breaks, it's leading spaces and it's tailing spaces into just one space.
the second part \s+ will shrink spaces into one space.
then trim() removes the leading/tailing space.