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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18526979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:43:33  来源:igfitidea点击:

Remove backslash \ from string using preg replace of php

phpregexreplacepreg-replacestripslashes

提问by Vignesh Pichamani

I want to remove the backslash alone using php preg replace.

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

For example: I have a string looks like

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

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

How to remove the \alone from the corresponding string using php preg_replace

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

回答by roninblade

why would you use preg_replace when str_replace is much easier.

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

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

回答by Bora

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

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

echo preg_replace('/\\/', '', $var);

回答by nim

You can also use stripslashes() like,

您还可以使用 stripslashes() 之类的,

<?php echo stripslashes($var); ?>

回答by B.Asselin

$str = preg_replace('/\\(.?)/', '', $str);

回答by Srijeeta Ghosh

This worked for me!!

这对我有用!!

preg_replace("/\//", "", $input_lines);