Remove backslash \ from string using preg replace of php

Vignesh Pichamani picture Vignesh Pichamani · Aug 30, 2013 · Viewed 38.1k times · Source

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

Answer

roninblade picture roninblade · Aug 30, 2013

why would you use preg_replace when str_replace is much easier.

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