Php如何转义文件路径的字符

Php如何转义文件路径的字符,php,explode,html-escape-characters,Php,Explode,Html Escape Characters,我想用“\”分隔$filePath,但要使用转义字符。。 如何解决这个问题? 非常感谢您必须转义“\”字符。这是通过放置两次来实现的 $filePath="c:\tmp\2012\tmp\test.txt"; $array=explode("\",$filePath); foreach($array as $test){ echo $test; } 结果:\ 应用于您的代码: $string = "\\"; echo $string; 结果:c:\tmp\2012\tmp\tes

我想用“\”分隔$filePath,但要使用转义字符。。 如何解决这个问题? 非常感谢

您必须转义“\”字符。这是通过放置两次来实现的

$filePath="c:\tmp\2012\tmp\test.txt";

$array=explode("\",$filePath);

foreach($array as $test){
    echo $test;
}
结果:
\

应用于您的代码:

$string = "\\";
echo $string;
结果:
c:\tmp\2012\tmp\test.txt

指定路径时,也可以使用单引号而不是双引号。这是我的建议

$filePath="c:\\tmp\\2012\\tmp\\test.txt";
echo $filepath

结果:
c:\tmp\2012\tmp\test.txt

您需要使用单引号:

$filePath='c:\tmp\2012\tmp\test.txt';
echo $filepath
或双重逃逸:

$filePath='c:\tmp\2012\tmp\test.txt';
注意在
explode
调用中需要两个斜杠:

$filePath="c:\\tmp\2012\\tmp\\test.txt";

更容易使用
/
,因为它可以很好地处理windows路径。@JasonZ不客气。请选择其中一个答案作为“最佳”,并向上投票以表示您的感谢。即使你选择jszobody而不是我也没关系。但重点应该放在某个人身上。
$array=explode("\\",$filePath);