To encode a literal backslash, you need to execute it twice: once for a string and once for a regex engine:
preg_match('|\\\\tmp\\\\([A-Za-z0-9]+)|', $str, $matches);
In PHP (when using single-quoted strings), this only applies to real backslashes; other regex escape options in order with one backslash:
preg_match('/\bhello\b/', $subject)
This is described in the manual (see the box labeled βNote:β at the top of the page).
Tim pietzcker
source share