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
23 Dec 2020 at 03:40 PM
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);

0
give a positive ratinggive a negative rating
03 Jan 2021 at 01:55 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us