Search code examples
phpexplodehtml-escape-characters

Php how to escape character of file path


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

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

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

I want to separate the $filePath by "\", but the escape characters.. How to solve this? THank you very much


Solution

  • You either need to use single quotes:

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

    Or double escape:

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

    Note you need two slashes in your explode call:

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