14

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 Answers 5

23

why would you use preg_replace when str_replace is much easier.

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

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

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

You can also use stripslashes() like,

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

This worked for me!!

preg_replace("/\//", "", $input_lines);
1
  • This will capture slash but not backslash.
    – vess
    Sep 13, 2022 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.