Page 1 of 1

get the file name from a path string

Posted: Thu Aug 28, 2003 7:43 am
by yaron
hi,
I have a path of a file in that form
c:\folderA\folderB\folderC....\folderZ\file.txt
I need only the file name.
What regular expression I can use to get it?
Thank you!

Posted: Thu Aug 28, 2003 7:48 am
by Nay
str_replace("c:\\foldera\\folderb\\", "", $path);

there might be a better solution, but that's what i'd use...

-Nay

Posted: Thu Aug 28, 2003 7:58 am
by volka

Code: Select all

<?php
$path = 'c:\\folderA\\folderB\\folderC\\folderZ\\file.txt';

echo basename($path), " \n";

$i = pathinfo($path);
echo $i['basename'], " \n";;

echo strrchr($path, '\''), " \n";;

preg_match('/([^\\\\]*(?!\\\\))$/', $path, $match);
print_r($match);
?>
choose one ;)