Search code examples
phppreg-matchfilepath

PHP Compare two filepaths for match preg_match


I am trying to compare two filepaths. I receive the filepath from a db query, and I need to find in the .m3u file for a match I have the following code which is not working properly. If the two filepaths are a match, then return the index from the $contents array as a pointer.

 $searching = '/home/Music/Pink Floyd-Dark Side Of The Moon(MFSL Dr. Robert)/06 - Money.flac'
 $a_search = pathinfo( $searching ); 

 
 $contents = file('/home/playlist/music.m3u'); 

 foreach( $contents as $index => $line ) {
            
            $a_line = pathinfo( $line );
            
            $searchbasename = preg_quote($a_search['dirname'] );
            $linebasename   = preg_quote($a_line['dirname'] );

            if( array_key_exists('dirname', $a_line)){
                if (preg_match("/\b$searchbasename\b/", $linebasename)) {
                   return $index;
                }
            }
        }

Basically, I need to compare two filepaths, and if they match, return the index of the $contents array.

Thanks in advance, for your time.

A portion of the .m3u file

/home/scott/Music/U2/U2 - War [FLAC]/03 - New Year's Day.flac
/home/scott/Music/U2/U2 - The Unforgettable Fire [FLAC]/02.Pride.flac
/home/scott/Music/ZZ Top/(1972) ZZ Top - Rio Grande Mud/02 Just Got Paid.flac
/home/scott/Music/ZZ Top/(1981) ZZ Top - El Loco/08 Groovy Little Hippie Pad.flac
/home/scott/Music/ZZ Top/(1979) ZZ Top - Deguello/01 I Thank You.flac
/home/scott/Music/ZZ Top/(1973) ZZ Top - Tres Hombres/03 Beer Drinkers & Hell Raisers.flac
/home/scott/Music/ZZ Top/(1976) ZZ Top - Tejas/02 Arrested for Driving While Blind.flac
/home/scott/Music/ZZ Top/(1983) ZZ Top - Eliminator/08 TV Dinners.flac
/home/scott/Music/ZZ Top/(1973) ZZ Top - Tres Hombres/02 Jesus Just Left Chicago.flac
/home/scott/Music/ZZ Top/(1979) ZZ Top - Deguello/04 A Fool For Your Stockings.flac
/home/scott/Music/ZZ Top/(1979) ZZ Top - Deguello/03 I'm Bad, I'm Nationwide.flac
/home/scott/Music/ZZ Top/(1981) ZZ Top - El Loco/01 Tube Snake Boogie.flac

Solution

  • It seems to me that you are not able to match the whole directory path. Rather, you want to match the album directory followed by the flac file name. In other words, you want to match the last directory and the file name as a string.

    You could explode and slice and rejoin the parts of the file path, but I prefer to use regex to extract the substring in a single call. My preg_replace() call will remove all characters except for the final directory and filename.

    $searching = '/home/Music/Pink Floyd-Dark Side Of The Moon(MFSL Dr. Robert)/06 - Money.flac';
    $needle = preg_replace('~/(?:[^/]*/)*(?=[^/]*/[^/]*$)~', '', $searching);
    // or     implode('/', array_slice(explode('/', $searching), -2)); // ...if you don't like regex
    
    foreach(file('/home/playlist/music.m3u') as $index => $line) {
        if (strpos($line, $needle) !== false) {
            return $index;
        }
    }