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

时间:2015-03-11 18:48:31

标签: php explode html-escape-characters

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

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

foreach($array as $test){
    echo $test;
}

我想将$ filePath分隔为“\”,但是转义字符.. 怎么解决这个? 非常感谢你

2 个答案:

答案 0 :(得分:3)

你必须逃避“\”字符。这可以通过放置两次来完成。

$string = "\\";
echo $string;

结果:\;

申请代码:

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

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

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

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

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

答案 1 :(得分:3)

您需要使用单引号:

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

或双重逃避:

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

请注意,explode来电需要两个斜杠:

$array=explode("\\",$filePath);
相关问题