views:

67

answers:

4

Hi , really simple question how can I preg_replace the backslash charactor ?

A: 

Escape \ with \: \\

preg_replace('/\\/', 'REMOVED BACKSLASH', 'sometest\othertest');
Marek Karbarz
+1  A: 

You need to escape the backslash: \\

From the manual on preg_replace():

To use backslash in replacement, it must be doubled ("\\\\" PHP string). 

Alternatively, use preg_quote to prepare a string for a preg_* operation.

Pekka
+1  A: 

Use it twice eg \\

Sarfraz
+1  A: 

Yes, but you need to escape it. When using it in the regexp use \\ to use it in the replacement, use \\\\ (that will turn into \\ that will be interpreted as a single backslash).

Johnco