使用preg替换php从字符串中删除反斜杠\

Vig*_*ani 9 php regex replace preg-replace stripslashes

我想使用php preg replace来删除反斜杠.

例如:我有一个字符串看起来像

$var = "This is the for \testing and i want to add the # and remove \slash alone from the given string";
Run Code Online (Sandbox Code Playgroud)

如何\使用php从相应的字符串中删除单独preg_replace

ron*_*ade 20

当str_replace更容易时,你为什么要使用preg_replace?

$str = str_replace('\\', '', $str);
Run Code Online (Sandbox Code Playgroud)


Bor*_*ora 15

要替换使用反斜杠,必须\\\\preg_replace中加倍(PHP字符串)

echo preg_replace('/\\\\/', '', $var);
Run Code Online (Sandbox Code Playgroud)


小智 11

你也可以使用stripslashes()之类的,

<?php echo stripslashes($var); ?>
Run Code Online (Sandbox Code Playgroud)