Update
function replaceWhitespace($str) { $result = $str; foreach (array( " ", " \t", " \r", " \n", "\t\t", "\t ", "\t\r", "\t\n", "\r\r", "\r ", "\r\t", "\r\n", "\n\n", "\n ", "\n\t", "\n\r", ) as $replacement) { $result = str_replace($replacement, $replacement[0], $result); } return $str !== $result ? replaceWhitespace($result) : $result; }
compared with:
preg_replace('/(\s)\s+/', '$1', $str);
The manual function works about 15% faster with very long (300 kb +) lines.
(at least in my car)
Yoshi source share