Seaching for a pattern from the end of a string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
plezops
Forum Newbie
Posts: 11
Joined: Mon May 28, 2007 5:22 pm
Location: Austin, Texas

Seaching for a pattern from the end of a string

Post by plezops »

I have the code

Code: Select all

$fileName 	= basename($_FILES['uploadedFile']['name']);
$fileExt	= eregi_replace('^.+\\.([^.]+)$', '\\1' , $fileName);
This works fine, what it does is looks for a period and then captures everything after that period, giving the file's extension. This will not give the extension though if there is another period in the file name. Is there a way to start from the end and work backwards then once a period is found it will capture everything at the end of the string like it currently does?

Another question is how I can separate the pieces of info into two different strings example: test.jpg is passed in as $fileName then I do the pattern search and break it up into 2 different strings one called $fileExt which would contain 'jpg' (no period, no ') and $fileName which would equal 'test' (again no period and no')

Any help in solving these 2 problems would be greatly appreciated!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Post Reply