separate file name into pieces

Discussion in 'PHP' started by bumbar, Nov 9, 2012.

  1. #1
    Hello!

    I have name of file like this: fennecs\modules\bankwire\bankwire.php

    yes, this is the real name of the file fennecs\modules\bankwire\bankwire.php, not file path.

    How to use explode() or some ereg function.
    So that they can be divided with "\".
    In this situation, I want to get file "bankwire.php" and create directories "fennecs", "modules", "bankwire"

    I use this:

    
    $pieces = explode('\'', "fennecs\modules\bankwire\bankwire.php");
    print_r ($pieces);
    
    PHP:
    but no result...

    I use this too: (\ is backslash Special Characters in HTML)
    
    $pieces = explode('\'', "fennecs\modules\bankwire\bankwire.php");
    print_r ($pieces);
    
    PHP:
    but no result too ...

    Suggestion?
    Thank you!
     
    Solved! View solution.
    bumbar, Nov 9, 2012 IP
  2. #2
    A backslash is an escape character in PHP. If you want to refer to a backslash, you have to escape the backslash with a backslash. Like this.

    
    <?php
    $string = 'fennecs\modules\bankwire\bankwire.php';
    
    $split = explode('\\', $string);
    
    print_r ($split);
    
    //Script outputs this:
    
    //Array (     [0] => fennecs     [1] => modules     [2] => bankwire     [3] => bankwire.php )
    ?>
    
    Code (markup):

    I tested it. It works. Notice the escaping of the backslash with a backslash (//). So the string you use as a boundary must be a double backslash. '\\'
     
    billzo, Nov 10, 2012 IP