Replace backslashes with PHP str_replace or regex preg_replace

How can I replace the backslashes in PHP with functions str_replace or regex preg_replace ?
23 Dec 2020 at 03:40 PM
0pnshow more
StorStor
Hi,

To replace backslash for example with forwardslash using str_replace, you have to use double backslash:

$value = "\A\B\C";
$updated_value = str_replace("\\", "/", $value);

When using regular expression with preg_replace, you can do something like this:

$value = "\A\B\C";
$pattern = "/[\\\w]/";
$updated_value = preg_replace($pattern, "/", $value);

03 Jan 2021 at 01:55 PM
0pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook & Twitter
2021 AnswerTabsTermsContact us