Home > front end >  Is there an easy way to get directory names without full path in Laravel?
Is there an easy way to get directory names without full path in Laravel?

Time:10-13

I'm trying to get all folder names from a sub folder. I used the following :

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

And the result looks like :

[
     "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",
   ]

Is there a way to get only the folder names without the "resources/lang/" part ? (E.g : nl, no, pt, etc.)

CodePudding user response:

As the base directory is always the same, you can use array_map() to remove that part.

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