Yup, seems to work now I used this to trim the spaces from the beginning and the end:
PHP Code: 
    $result trim($result); 
But that brings up another problem, there are extra hyphens at the beginning and the end after the numbers/words under 3chars have been deleted. It needs to trim any remaining hyphens from the beginning and end too, before trimming the whitespace. I tried these lines but neither worked:
PHP Code: 
    $result trim($result"-"); 
PHP Code: 
    $result preg_replace('/[-]*(\r{0,1}\n)[-]*/''$1'$result);