When testing an answer for another user\'s question I found something I don\'t understand. The problem was to replace all literal \\t \\n \\r
Its works in perl because you pass that directly as regex pattern /(?:\\[trn])+/
but in php, you need to pass as string, so need extra escaping for backslash itself.
"/(?:\\\\[trn])+/"
The regex \ to match a single backslash would become '/\\\\/' as a PHP preg string
You need 4 backslashes to represent 1 in regex because:
"\\\\" -> \\)\\ -> \)From the PHP doc,
escaping any other character will result in the backslash being printed too1
Hence for \\\[,
\, one stay because \[ is invalid ("\\\[" -> \\[)\\[ -> \[)Yes it works, but not a good practice.
The regular expression is just /(?:\\[trn])+/. But since you need to escape the backslashes in string declarations as well, each backslash must be expressed with \\:
"/(?:\\\\[trn])+/"
'/(?:\\\\[trn])+/'
Just three backspaces do also work because PHP doesn’t know the escape sequence \[ and ignores it. So \\ will become \ but \[ will stay \[.
Use str_replace!
$code = str_replace(array("\t","\n","\r"),'',$code);
Should do the trick