Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php中的str_replace()替换反斜杠和正斜杠_Php - Fatal编程技术网

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

用php中的str_replace()替换反斜杠和正斜杠,php,Php,我有以下网址: $str = "http://www.domain.com/data/images\flags/en.gif"; 我正在使用str\u replace尝试将反斜杠替换为正斜杠: $str = str_replace('/\/', '/', $str); 这似乎不起作用,结果是: http://www.domain.com/data/images\flags/en.gif 没有正则表达式,因此不需要// 这应该起作用: $str = str_replace("\\", '/'

我有以下网址:

$str = "http://www.domain.com/data/images\flags/en.gif";
我正在使用
str\u replace
尝试将反斜杠替换为正斜杠:

$str = str_replace('/\/', '/', $str);
这似乎不起作用,结果是:

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

没有正则表达式,因此不需要//

这应该起作用:

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

您还需要转义“\”。

要替换反斜杠吗

试试条纹睫毛:


您必须放置双反斜杠

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

你需要用一个空格来避开反斜杠\

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

单引号php字符串变量有效

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

@Subdigger:从技术上讲,这是不对的,但你可以再等一段时间让他修复代码…@genesis你的答案和他的一样。请看下面,你也需要转义“\”。我认为
stru replace()的语法
已更改,因此,
$str
现在应该是第一个参数。@subfigger:发布软件将我的双斜杠替换为一,因为我忘记使用“code”块。你投了反对票,然后发布了完全相同的解决方案,这真是太差劲了。@Sylverdrag:实际上Subdigger是第一个给出答案的海报;)@创世纪:该死。我发帖时没有看到任何答案,但这件事进展得太快了……这删除了斜杠,而不是像问题要求的那样替换它们。另外,请链接到PHP手册的英文版本此答案在接受答案之前;)应标记为正确的,因为它是相同的;)
$str = 'http://www.domain.com/data/images\flags/en.gif';
$str = str_replace('\\', '/', $str);