在Laravel中有没有一种简单的方法来获取没有完整路径的目录名?

watbbzwu  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(108)

我正在尝试从子文件夹中获取所有文件夹名称。我使用了以下命令:

$lang = File::directories('resources/lang/');

字符串
结果如下:

[
     "resources/lang/da",
     "resources/lang/de-CH",
     "resources/lang/de-ES",
     "resources/lang/en",
     "resources/lang/en-AE",
     "resources/lang/en-ES",
     "resources/lang/en-GB",
     "resources/lang/en-PT",
     "resources/lang/es",
     "resources/lang/es-PE",
     "resources/lang/fi",
     "resources/lang/fr",
     "resources/lang/fr-CH",
     "resources/lang/fr-ES",
     "resources/lang/it-CH",
     "resources/lang/nl",
     "resources/lang/no",
     "resources/lang/pt",
     "resources/lang/sk",
     "resources/lang/sv",
   ]


有没有一种方法可以只获取文件夹名而不包含“resources/lang/”部分?(例如:nl、no、pt等)

u91tlkcl

u91tlkcl1#

由于基目录始终相同,因此可以使用array_map()删除该部分。

$lang = array_map(fn($value) => str_replace('resources/lang/', '', $value), $lang);

字符串

oxosxuxt

oxosxuxt2#

您可以将array_map与一个标准回调函数一起使用,该回调函数将返回path的尾随名称部分

$lang = array_map('basename', File::directories('resources/lang/'));

字符串
结果看起来

[
 "da",
 "de-CH",
 "de-ES",
 "en",
 "en-AE",
 "en-ES",
 "en-GB",
 "en-PT",
 "es",
 "es-PE",
 "fi",
 "fr",
 "fr-CH",
 "fr-ES",
 "it-CH",
 "nl",
 "no",
 "pt",
 "sk",
 "sv",
]


@见https://www.php.net/manual/en/function.basename.php

相关问题