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
Replacing backslashes with forward slashes with str_replace() in php
提问by Hai Truong IT
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_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: