1.去除字符中所有空格(包字符串中间空格)
$search = array(" "," ","\n","\r","\t");
$replace = array("","","","","");
$str = str_replace($search, $replace, $str);
2.去除字符中头尾部空格(全角半角)
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace(' ', "\n ", $str);