RegEx / Regular Expression for filename and file extension

This is a helpful snippet that I have found myself searching for in the google machine. Hopefully this helps you out!


$filename = "thisismyfilename.ext";

preg_match("/(.*)\.([^\.]+)$/", $filename,  $matches);

print_r($matches);

//Result:
array([0]=>'thisismyfilename.ext',
        [1]=>'thisismyfilename',
        [2]=>'ext')

Wednesday, April 7th, 2010 PHP

Leave a Reply

You must be logged in to post a comment.

[ad code=1 align=center]