【问题标题】:Php how to escape character of file pathphp如何转义文件路径的字符
【发布时间】:2015-03-11 18:48:30
【问题描述】:
$filePath="c:\tmp\2012\tmp\test.txt";

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

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

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

【问题讨论】:

  • 更容易使用/,因为它适用于 Windows 路径。

标签: php explode html-escape-characters


【解决方案1】:

你要么需要使用单引号:

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

或双重转义:

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

请注意,explode 调用中需要两个斜杠:

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

【讨论】:

    【解决方案2】:

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

    $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

    【讨论】:

    • @JasonZ 不客气。请选择其中一个答案作为“最佳”并投票以表示您的赞赏。即使您选择 jszobody 而不是我也可以。但重点应该归于某人。
    猜你喜欢
    • 2023-03-21
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多