Php regex replace double backslash with one

I do not want to use stripslashes(), because I want to replace "\\" with "\".

I tried preg_replace("/\\\\/", "\\", '2\\sin(\\pi s)\\Gamma(s)\\zeta(s) = i\\oint_C \\frac{-x^{s-1}}{e^x -1} \\mathrm{d}x');

Which returns to my disappointment: 2\\sin(\\pi s)\\Gamma(s)\\zeta(s) = i\\oint_C \\frac{-x^{s-1}}{e^x -1} \\mathrm{d}x

Various online regexp testers indicate that the above should work. Why is this not so?

+4
source share
2 answers

First, like many other people, regular expressions may be too heavy for a tool to work with, but the solution you use should work.

$newstr = preg_replace('/\\\\/', '\\', $mystr);

, , preg_replace , , .

str_replace :

$newstr = str_replace('\\\\', '\\', $mystr);

, , .

+7

,

$newstr = str_replace("\\\\", "\\", $mystr);

. str_replace

+7

All Articles