使用php的preg replace从字符串中删除反斜杠

使用php的preg replace从字符串中删除反斜杠,php,regex,replace,preg-replace,stripslashes,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"; 如何使用phppreg\u replace从相应字符串中单独删除\,当str\u replace更容易时,为什么要使用preg\u replace $str = str_replace('\\', ''

我想使用php preg replace单独删除反斜杠

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

$var = "This is the for \testing and i want to add the # and remove \slash alone from the given string";

如何使用php
preg\u replace

从相应字符串中单独删除
\
,当str\u replace更容易时,为什么要使用preg\u replace

$str = str_replace('\\', '', $str);

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


您还可以使用stripslashes(),例如


这对我很有效

preg_replace("/\//", "", $input_lines);
<?php echo stripslashes($var); ?>
$str = preg_replace('/\\\\(.?)/', '$1', $str);
preg_replace("/\//", "", $input_lines);