[php] Replacing backslashes with forward slashes with str_replace() in php

I have the following url:

$str = "http://www.domain.com/data/images\flags/en.gif";

I'm using str_replace to try and replace backslashes with forward slashes:

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

It doesn't seem to work, this is the result:

http://www.domain.com/data/images\flags/en.gif

This question is related to php

The answer is


you have to place double-backslash

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

You need to escape backslash with a \

  $str = str_replace ("\\", "/", $str);

No regex, so no need for //.

this should work:

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

You need to escape "\" as well.


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

You want to replace the Backslash?

Try stripcslashes:

http://www.php.net/manual/en/function.stripcslashes.php


Single quoted php string variable works.

$str = 'http://www.domain.com/data/images\flags/en.gif';
$str = str_replace('\\', '/', $str);