Remove backslash \ from string using preg replace of php

蓝咒 提交于 2019-12-08 17:13:42

问题


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


回答1:


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

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



回答2:


why would you use preg_replace when str_replace is much easier.

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



回答3:


You can also use stripslashes() like,

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



回答4:


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



回答5:


This worked for me!!

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


来源:https://stackoverflow.com/questions/18526979/remove-backslash-from-string-using-preg-replace-of-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!