Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
目录的PHP正则表达式_Php_Regex - Fatal编程技术网

目录的PHP正则表达式

目录的PHP正则表达式,php,regex,Php,Regex,我需要一个正则表达式,它将接受最后一个正斜杠后的字符串 例如,考虑到我有以下字符串: C:/dir/file.txt 我只需要获取file.txt部分字符串 谢谢:您不需要正则表达式 $string = "C:/dir/file.txt"; $filetemp = explode("/",$string); $file = end($filetemp); 之所以编辑,是因为我记得最新的PHP在链接这些类型的函数时抛出了错误。strrpos函数查找最后出现的字符串。您可以使用它来确定文件名

我需要一个正则表达式,它将接受最后一个正斜杠后的字符串

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

C:/dir/file.txt
我只需要获取file.txt部分字符串


谢谢:

您不需要正则表达式

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

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

$file = end($filetemp);
之所以编辑,是因为我记得最新的PHP在链接这些类型的函数时抛出了错误。

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

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

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

例如:

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

$file = basename($string);
否则,其他答案都很好