11

I want to split

$path = getenv('PATH');

into its components. How do I determine the separator char in an os-dependent fashion?

fabiangebert
  • 2,623
  • 2
  • 22
  • 25

4 Answers4

21

You can use the PATH_SEPARATOR constant, then the DIRECTORY_SEPARATOR constant to split the path if needed. See Directory Predefined Constants

Greg
  • 316,276
  • 54
  • 369
  • 333
  • 5
    You cannot use `DIRECTORY_SEPARATOR` for that. You must use `PATH_SEPARATOR`. First one is the character that separates folders from each other, path separator separates differents paths i.e. defined in PATH environmental variable. – RaYell Aug 11 '09 at 10:09
  • @Greg, ? Would you at least clean up the mistake or delete the answer so that others below can get rightful attention? Your answer with 18 upvotes is wrong. – Pacerier Jul 15 '15 at 07:59
4

Use the PATH_SEPARATOR constant.

RaYell
  • 69,610
  • 20
  • 126
  • 152
3

I know this works for the include_path - not sure about getenv('PATH'):

$paths = split(PATH_SEPARATOR, getenv('PATH'));
gnarf
  • 105,192
  • 25
  • 127
  • 161
  • include_path is a valid PATH style string for whatever system you are on: the same split techniques that work for one will work for the other. – Matthew Scharley Aug 11 '09 at 10:10
  • 1
    Note that this post was written in 2009. `split()` has been deprecated according to the PHP manual. – mauris Dec 17 '12 at 21:48
0

I seem to remember that Windows will accept both forward- and back-slashes as a file-separator, so you may not have to worry about it.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 1
    I think he means the separator for the Entries in the PATH environment variable (e.g. ":" on *nix), not the separator inside the paths. – c089 Aug 11 '09 at 10:24