How to extract part of the path and the ending file name with Regex?

Posted by brasofilo on Stack Overflow See other posts from Stack Overflow or by brasofilo
Published on 2012-12-05T22:39:42Z Indexed on 2012/12/05 23:03 UTC
Read the original article Hit count: 221

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about regex