get a file name from a path.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

get a file name from a path.

Post by yaron »

Hi,
I have to write a function that takes the path and gets the file name
the path can come in this format:
'c:\\folderA\\folderB\\folderC\\folderZ\\file.txt'
Or in that format:
'c:/folderA/folderB/folderC/folderZ/file.txt'
Here is the script I use now:
preg_match('/([^\\\\]*(?!\\\\))$/', $path, $match);
$result = array_unique ($match);
print_r($result);
but it helps only for the first case. how can I write a small script that handle both?
Thanks
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

I think the PHP function basename() will help you out a bit.
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

thanks
that is what i was missing
Post Reply