php 在php中用str_replace()用正斜杠替换反斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6806597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:18:27  来源:igfitidea点击:

Replacing backslashes with forward slashes with str_replace() in php

php

提问by Hai Truong IT

I have the following url:

我有以下网址:

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

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

我正在str_replace尝试用正斜杠替换反斜杠:

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

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

它似乎不起作用,这是结果:

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

回答by genesis

you have to place double-backslash

你必须放置双反斜杠

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

回答by Subdigger

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

回答by Sylverdrag

No regex, so no need for //.

没有正则表达式,所以不需要 //。

this should work:

这应该有效:

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

You need to escape "\" as well.

您还需要转义“\”。

回答by Hadu

You need to escape backslash with a \

你需要用 \ 转义反斜杠

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

回答by saravanabawa

Single quoted php string variable works.

单引号 php 字符串变量有效。

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

回答by Michele

You want to replace the Backslash?

你想替换反斜杠?

Try stripcslashes:

尝试stripcslashes:

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

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