-1

Possible Duplicate:
How to extract a file extension in PHP?

I need to build an associative array with the plugin name and the language file it uses in the following sequence:

/whatever/path/length/public_html/wp-content/plugins/adminimize/languages/adminimize-en_US.mo
/whatever/path/length/public_html/wp-content/plugins/audio-tube/lang/atp-en_US.mo
/whatever/path/length/public_html/wp-content/languages/en_US.mo
/whatever/path/length/public_html/wp-content/themes/twentyeleven/languages/en_US.mo 

Those are the language files WordPress is loading.
They are all inside /wp-content/, but with variable server paths.
I'm looking only for those inside the plugins folder, grab the plugin folder name and the filename.

Hipothetical case in PHP, where reg_extract_* functions are the parts I'm missing:

$plugins = array();

foreach( $big_array as $item ) 
{
    $folder = reg_extract_folder( $item );
    if( 'plugin' == $folder ) 
    {
        // "folder-name-after-plugins-folder"
        $plugin_name = reg_extract_pname( $item ); 

        // "ending-mo-file.mo"
        $file_name = reg_extract_fname( $item ); 

        $plugins[] = array( 'name' => $plugin_name, 'file' => $file_name );
    }
}

[update]

Ok, so I was missing quite a basic function, pathinfo... :/

No problem to detect if /plugins/ is contained in the array.

But what about the plugin folder name?

Community
  • 1
  • 1
brasofilo
  • 25,496
  • 15
  • 91
  • 179

2 Answers2

2

This example will get all the files within a folder, and display all the information about the file path.

<?php

foreach(glob("/path/tofiles/*.*") as $file){
    $info = pathinfo($file);
    echo $info['dirname'], "\n";
    echo $info['basename'], "\n";
    echo $info['extension'], "\n";
    echo $info['filename'], "\n"; // since PHP 5.2.0
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • Thanks for that... promise not to forget `pathinfo` never again... Unfortunately, it doesn't grab all the info I need. Updated the Q. – brasofilo Dec 05 '12 at 23:01
1
$path = '/whatever/path/length/public_html/wp-content/plugins/adminimize/languages/adminimize-en_US.mo';
preg_match('/plugins\/([^\/]+?)\/(?:[^\/]+\/)?(.+)/', $path, $matches);
$dir = $matches[1];
$filename = $matches[2];

this would store adminize in $dir and adminimize-en_US.mo in $filename

Brian Cray
  • 1,277
  • 1
  • 7
  • 19