$text = preg_replace('/[\s\r\n\t]/', ' ', $text); What special symbols will be removed? What does each letter (s, r, n, t) represent? s - white spaces?
You need to change the single quotes to double quotes: "" because.... \n is a newline; \r is a carriage return \t is a tab \s is "any whitespace" so it will match a space, \t, \r or \n For clarification: on UNIX/Mac, lines end with \n; on windows it's \r\n, and I think in some older OSes lines ended in \r. If it's in single quotes, it's treated as a literal backslash, and then r or n or t, etc. If its in double quotes, these will be treated as newline/carriage return/tab So it should be: $text = preg_replace("/[\s\r\n\t]/", ' ', $text); And if you want to trim it down, just: $text = preg_replace('/\s/', ' ', $text);
Single quotes are just fine, and they are always recommended for clarity (backslash and r when used together are not treated as separate characters within single quotes for regexp matching). The original expression will work as expected. It will remove all whitespace from the text. Double quotes are very confusing for newbies for regexp matching. For example '/[\\]/' will match a backslash but "/[\\]/" wont even compile resulting in error (missing terminating ]) while it looks pretty valid. To the thread starter: this regexp will replace all whitespace characters, new lines, cariage returns and tabs with '' (i.e will remove them). Just '/[\s]/' will do the same.