Forse non ho letto bene, ma penso si potrebbe rimpiazzare gran parte del codice di doHYPEN con

Codice PHP:
                $hyphen[0]=substr($string0$n+1);
                if (
$a != " ") { $hyphen[0] .="-";}                
                
$hyphen[1]=substr($string$n+1);                
                return 
$hyphen