Replacing backslashes with forward slashes with str_replace() in php

Php

Php Problem Overview


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

Php Solutions


Solution 1 - Php

you have to place double-backslash

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

Solution 2 - Php

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

Solution 3 - Php

No regex, so no need for //.

this should work:

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

You need to escape "" as well.

Solution 4 - Php

You need to escape backslash with a \

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

Solution 5 - Php

Single quoted php string variable works.

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

Solution 6 - Php

You want to replace the Backslash?

Try stripcslashes:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHai Truong ITView Question on Stackoverflow
Solution 1 - PhpgenesisView Answer on Stackoverflow
Solution 2 - PhpSubdiggerView Answer on Stackoverflow
Solution 3 - PhpSylverdragView Answer on Stackoverflow
Solution 4 - PhpHadi SharghiView Answer on Stackoverflow
Solution 5 - PhpsaravanabawaView Answer on Stackoverflow
Solution 6 - PhpMicheleView Answer on Stackoverflow