SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex'ing file paths

    I seem to be having trouble running regex against file paths. I am doing a preg_match of this string: C:\xampp\htdocs\test\cache using the following regex: /(C:\xampp\htdocs\test\modules|C:\xampp\test\sugar52pro\themes|C:\xampp\htdocs\test\cache)/

    This is an output form an echo so there is only one slash and a directory separator. In the debug they are all double slashes. PHP does not find a match but if I put these into a regex editor I get a match.

    What could I be doing wrong?

    Does anyone know of a good article about making paths work for Linux and Windows server that includes best practices?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is whitespace in here
    C:\xampp\htdocs\ test\cache

    Otherwise, you need to post a runnable snippit of php code which demonstrates the problem.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is actually supposed to be C:\xampp\htdocs\test\cache. Test was another name which included client info that I changed while posting.

    I am using a full_copy function from the php website and trying to modify it to filter out certain folders during the copy. Here is the function where everything is going wrong. If you know a better way to do anything shown then please let me know.

    PHP Code:
     function full_copy$source$target$base_url )
        {                                   
            
            
    $file_patterns = array('/','\\','//','\\/');
            
    //$files_ar = array();

            
    $excludes = array(
                
    //$base_url . '\/modules\/Accounts',
                //$base_url . '\/modules\/ACL',
                //$base_url . '\/modules\/ACLActions',
                //$base_url . '\/modules\/ACLFields',
                //$base_url . '\/modules\/ACLRoles',
                //$base_url . '\/modules\/Activities',
                //$base_url . '\/modules\/Administration',
                //$base_url . '\/modules\/Audit',
                //$base_url . '\/modules\/Bugs',
                //$base_url . '\/modules\/Calendar',
                //$base_url . '\/modules\/Calls',
                //$base_url . '\/modules\/CampaignLog',
                //$base_url . '\/modules\/Campaigns',
                //$base_url . '\/modules\/CampaignTrackers',
                //$base_url . '\/modules\/Cases',
                //$base_url . '\/modules\/Charts',
                //$base_url . '\/modules\/Configurator',
                //$base_url . '\/modules\/Connectors',
                //$base_url . '\/modules\/Contacts',
                //$base_url . '\/modules\/Contracts',
                
    $base_url '\\modules',
                
    $base_url '\\themes',
                
    $base_url '\\cache',

                );
            
    $excludes str_replace($file_patterns,'\\',$excludes);
            
    $regex '/(' .implode('|'$excludes) .')/';
            
            
    //prep target path
            
    $target str_replace($file_patterns,'\\',$target);
            
            if ( 
    is_dir$source ) )
            {
                @
    mkdir$target );
               
                
    $d dir$source );
               
                while ( 
    FALSE !== ( $entry $d->read() ) )
                {
                    if ( 
    $entry == '.' || $entry == '..' )
                    {
                        continue;
                    }
                   
                    
    $Entry $source '\\' $entry;   
                    
    $Entry str_replace($file_patterns,'\\',$Entry);         
                    if ( 
    is_dir$Entry ) )
                    {
                        
                    
    //DebugBreak();    

                        
    preg_match($regex,$Entry,$matches);

                        echo 
    $Entry "  " $regex "</br>" ;
                        if(empty(
    $matches))
                        {
                            
    full_copy$Entry$target '\\' $entry$base_url );
                        }
                        else
                        {
                            
    //DebugBreak();
                        
    }
                        
    //$files_ar[] = $Entry;
                        
    continue;
                    }
                    

                    
    preg_match($regex,$Entry,$matches);
                    
                    
                    if(empty(
    $matches));
                    {
                        
    copy$Entry$target '\\' $entry );
                    }
                    
    //$files_ar[] = $Entry;
                
    }
               
                
    $d->close();
            }else
            {
                
    copy$source$target );
                
    //$files_ar[] = $source;
            
    }
            
            
    //return $files_ar;
            
        


  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    I imagine the space is a result of an addaspacetoanyextremelylongwordthatwillnotwordwrapcorrectlyandtherebybreakthelayout type of thing.

    The double slashes are most likely a single slash preceded by an "escaping" slash.

    I have run into this problem before and I don't know how to solve it for "portable" code other than assigning the systems slash type to a variable and using the variable in the code.

    But if this is for your use only you could try hard-coding the slash into the code. First you'll need to change the regex's delimiter to something other than a forward slash so you can use it in the regex. eg.

    #/test/modules|test/sugar52pro/themes|test/cache#

    AFAIK, the code is working within the localhost server, not the Windows system, so you don't need the "C:.....htdocs" part.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I imagine the space is a result of an addaspacetoanyextremelylongwordthatwillnotwordwrapcorrectlyandtherebybreakthelayout type of thing.

    The double slashes are most likely a single slash preceded by an "escaping" slash.
    Yes they are.

    Quote Originally Posted by Mittineague View Post
    I have run into this problem before and I don't know how to solve it for "portable" code other than assigning the systems slash type to a variable and using the variable in the code.

    But if this is for your use only you could try hard-coding the slash into the code. First you'll need to change the regex's delimiter to something other than a forward slash so you can use it in the regex. eg.

    #/test/modules|test/sugar52pro/themes|test/cache#

    AFAIK, the code is working within the localhost server, not the Windows system, so you don't need the "C:.....htdocs" part.
    This code really did not like me getting rid of the full path.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I used this code, it correctly skips excluded files. Set the third parameter ($base) to false and it will exclude all those even in the excludes...also it might be worth it to pull $excludes out and put that as a param as well.
    PHP Code:
    function full_copy$source$target$base true )
    {
        if ( !
    file_exists$source )
        || ( 
    $source realpath$source ) ) === false ) {
            
    trigger_error'Source path not found.' );
            return 
    false;
        }

        
    // Normalize Paths.
        
    $source preg_replace'~[\\\\/]+~''/'$source '/' );

        
    // Base Path For Excludes
        
    $base = ( $base !== false )
              ? ( ( 
    $base === true || empty( $base ) )
                  ? 
    $source $base )
              : 
    '';

        
    #-------------------------------------------------------------------------------
        // Exclude Files/Folders

        
    $excludes = array( 'modules/''themes/''cache/''aggregated.png' );

        
    $tmp_func create_function'$a''return preg_quote( $a, "~" );' );
        
    $excludes '~' preg_quote$base'~' )
                  . 
    '(?:' joinarray_map$tmp_func $excludes ), '|' ) . ')~';

        
    #-------------------------------------------------------------------------------

        
    if ( is_dir$source ) ) {
            if ( !
    file_exists$target )
            && ( @
    mkdir$target0777true ) === false ) ) {
                
    trigger_error'Target path could not be created.' );
                return 
    false;
            }

            
    $target preg_replace'~[\\\\/]+~''/'realpath$target ) . '/' );

            
    $job dir$source );
            while ( ( 
    $file $job->read() ) !== false ) {
                if ( 
    $file == '.' || $file == '..' ) continue;

                
    $full $source $file;

                
    #-------------------------------------------------------------------------------
                // Handle Directories

                
    if ( is_dir$full ) ) {
                    if ( 
    preg_match$excludes$full '/' ) ) continue;
                    
    call_user_func__FUNCTION__$full$target $file, empty( $base ) ? false $base );
                    continue;
                }

                
    #-------------------------------------------------------------------------------
                // Handle Files

                
    if ( preg_match$excludes$full ) ) continue;
                
    copy$full$target $file );
            }

            
    $job->close();
            return 
    true;
        }

        
    copy$source$target );
        return 
    true;

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I will give this function a try.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •