PHP-从文件路径中提取当前目录和文件名

时间:2018-10-22 09:21:24

标签: php regex

我需要打开它:

/home/me/public_html/folder1/folder2/folder3/script.php

进入:

folder3/script.php

到目前为止,我已经尝试使用:

(\/.+\/.+\.php)$

但是最终结果是返回整个路径。我在做什么错了?

2 个答案:

答案 0 :(得分:3)

$path='/home/me/public_html/folder1/folder2/folder3/script.php';
$pathinfo=pathinfo($path);
$a=explode(DIRECTORY_SEPARATOR,$pathinfo['dirname']);
$dir=end($a);
echo $dir.DIRECTORY_SEPARATOR.$pathinfo['basename'];

答案 1 :(得分:0)

<?php
$pathinfo=pathinfo('/home/me/public_html/folder1/folder2/folder3/script.php');
$a=explode('/',$pathinfo['dirname']);
echo $dir=end($a);
echo $pathinfo['basename'];
?>