从路径字符串中提取直接子目录
我需要从完整路径字符串中提取直接子目录的名称。
例如,假设我们有:
$str = "dir1/dir2/dir3/dir4/filename.ext";
$dir = "dir1/dir2";
那么 $str 路径中相对于 $dir 的子目录名称将为“dir3”。请注意,$dir 末尾永远不会有“/”。
所以函数应该是:
$subdir = getsubdir($str,$dir);
echo $subdir; // Outputs "dir3"
如果 $dir="dir1" 那么输出将为“dir2”。如果 $dir="dir1/dir2/dir3/dir4" 则输出将为“”(空)。如果 $dir="" 则输出将为“dir1”。等等..
目前这就是我所拥有的,并且它有效(据我测试过)。我只是想知道是否有更简单的方法,因为我发现我使用了很多字符串函数。也许有一些神奇的正则表达式可以在一行中完成此操作? (不幸的是,我不太擅长正则表达式)。
function getsubdir($str,$dir) {
// Remove the filename
$str = dirname($str);
// Remove the $dir
if(!empty($dir)){
$str = str_replace($dir,"",$str);
}
// Remove the leading '/' if there is one
$si = stripos($str,"/");
if($si == 0){
$str = substr($str,1);
}
// Remove everything after the subdir (if there is anything)
$lastpart = strchr($str,"/");
$str = str_replace($lastpart,"",$str);
return $str;
}
正如您所看到的,为了处理一些奇怪的情况(输入中没有“/”、输入为空等),它有点棘手。我希望这一切都是有道理的。欢迎任何帮助/建议。
更新(改变解决方案):
嗯,Alix Axel说得对。这是他的解决方案,稍作调整,使其符合我的确切要求(例如:它必须返回一个字符串,只应输出目录(而不是文件))
function getsubdir($str,$dir) {
$str = dirname($str);
$temp = array_slice(array_diff(explode('/', $str), explode('/', $dir)), 0, 1);
return $temp[0];
}
I need to extract the name of the direct sub directory from a full path string.
For example, say we have:
$str = "dir1/dir2/dir3/dir4/filename.ext";
$dir = "dir1/dir2";
Then the name of the sub-directory in the $str path relative to $dir would be "dir3". Note that $dir never has '/' at the ends.
So the function should be:
$subdir = getsubdir($str,$dir);
echo $subdir; // Outputs "dir3"
If $dir="dir1" then the output would be "dir2". If $dir="dir1/dir2/dir3/dir4" then the output would be "" (empty). If $dir="" then the output would be "dir1". Etc..
Currently this is what I have, and it works (as far as I've tested it). I'm just wondering if there's a simpler way since I find I'm using a lot of string functions. Maybe there's some magic regexp to do this in one line? (I'm not too good with regexp unfortunately).
function getsubdir($str,$dir) {
// Remove the filename
$str = dirname($str);
// Remove the $dir
if(!empty($dir)){
$str = str_replace($dir,"",$str);
}
// Remove the leading '/' if there is one
$si = stripos($str,"/");
if($si == 0){
$str = substr($str,1);
}
// Remove everything after the subdir (if there is anything)
$lastpart = strchr($str,"/");
$str = str_replace($lastpart,"",$str);
return $str;
}
As you can see, it's a little hacky in order to handle some odd cases (no '/' in input, empty input, etc). I hope all that made sense. Any help/suggestions are welcome.
Update (altered solution):
Well Alix Axel had it spot on. Here's his solution with slight tweaks so that it matches my exact requirements (eg: it must return a string, only directories should be outputted (not files))
function getsubdir($str,$dir) {
$str = dirname($str);
$temp = array_slice(array_diff(explode('/', $str), explode('/', $dir)), 0, 1);
return $temp[0];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给你:
编辑 - 万无一失的实施:
Here you go:
EDIT - Foolproof implementation:
如何将整个内容拆分为一个数组:
然后您应该能够使用 array_diff():
然后,获取直接子级很容易:
我现在无法测试它,但它应该可以工作。
How about splitting the whole thing into an array:
you should then be able to use array_diff():
then, getting the direct child is easy:
I can't test this right now but it should work.
这是一个类似的“短”解决方案,这次使用字符串函数而不是数组函数。如果没有从字符串中获取相应的部分,
getsubdir
将返回FALSE
。strtr
段是转义百分比的快速方法,这对sscanf
具有特殊含义。进行快速测试,以便您了解它的行为方式:
Here's a similar "short" solution, this time using string functions rather than array functions. If there is no corresponding part to be gotten from the string,
getsubdir
will returnFALSE
. Thestrtr
segment is a quick way to escape the percents, which have special meaning tosscanf
.And a quick test so you can see how it behaves: