PHP preg_replace \

Oliver Bayes-Shelton picture Oliver Bayes-Shelton · Feb 8, 2010 · Viewed 23.8k times · Source

Really simple question: how can I preg_replace the backslash character?

Answer

Johnco picture Johnco · Feb 8, 2010

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).