Home > PHP > PHP – regular expression get filename and extension

PHP – regular expression get filename and extension


//this function will return extension of file name supplied
function getFilename($filename){
    preg_match("/^(.+?)(\.([^\.]*))?$/", $f, $matches);
    return $matches;
}

//how to use
print_r(getFilename('test.txt'));

/* output
array('text.txt', 'test', '.txt', 'txt');
*/

Categories: PHP Tags: ,
  1. 30/06/2013 at 18:02

    Typically I would not read write-up upon information sites, however i wish to say that this write-up pretty forced myself to perform therefore! Ones way of writing has become shocked me personally. Appreciate it, very good post.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: