-1

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.)

  • `$partial = explode ("/",$lang);` then to finish `$partial[i][2]` – KamiKaze Oct 12 '22 at 13:57
  • You can use [`explode`](https://www.php.net/manual/en/function.explode.php) to split the string to an array, and the last element of the array would be the locale, or [`str_replace`](https://www.php.net/manual/en/function.str-replace.php) to strip our `resources/lang`, etc. Have you tried anything? What, specifically are you stuck on? – Tim Lewis Oct 12 '22 at 13:58
  • Just looking for the cleanest solution here – TheQuietOne Oct 12 '22 at 14:25

1 Answers1

1

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);
Lucas
  • 394
  • 2
  • 13