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 ?
0
give a positive ratinggive a negative rating
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);

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us