2013-08-24 51 views
0

我需要一个正则表达式,该字符串会在最后一个正斜杠之后使用该字符串。目录的PHP正则表达式

例如,考虑到我有以下字符串:

C:/dir/file.txt 

我需要只有file.txt的部分(串)。

谢谢:)

回答

5

你不需要一个正则表达式。

$string = "C:/dir/file.txt"; 

$filetemp = explode("/",$string); 

$file = end($filetemp); 

编辑,因为我记得有关链接这些类型的功能的最新PHP吐出错误。

+0

谢谢!这解决了我的问题。 – Keeper

1

strrpos()函数查找字符串的最后出现。您可以使用它来确定文件名的起始位置。

$path = 'C:/dir/file.txt'; 
$pos = strrpos($path, '/'); 
$file = substr($path, $pos + 1); 
echo $file; 
3

如果你的字符串总是路径,你应该考虑basename()函数。

例子:

$string = 'C:/dir/file.txt'; 

$file = basename($string); 

否则,其他的答案是伟大的!