Remove backslash \ from string using preg replace

2019-06-17 01:06发布

I want to remove the backslash alone using 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

5条回答
叛逆
2楼-- · 2019-06-17 01:20

why would you use preg_replace when str_replace is much easier.

$str = str_replace('\\', '', $str);
查看更多
Lonely孤独者°
3楼-- · 2019-06-17 01:28
$str = preg_replace('/\\\\(.?)/', '$1', $str);
查看更多
三岁会撩人
4楼-- · 2019-06-17 01:33

You can also use stripslashes() like,

<?php echo stripslashes($var); ?>
查看更多
别忘想泡老子
5楼-- · 2019-06-17 01:35

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

echo preg_replace('/\\\\/', '', $var);
查看更多
家丑人穷心不美
6楼-- · 2019-06-17 01:40

This worked for me!!

preg_replace("/\//", "", $input_lines);
查看更多
登录 后发表回答