How do I properly split a PATH variable in PHP?

10,743

Solution 1

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

Solution 2

Use the PATH_SEPARATOR constant.

Solution 3

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

$paths = split(PATH_SEPARATOR, getenv('PATH'));
Share:
10,743
fabiangebert
Author by

fabiangebert

SaaS founder and passionate software developer

Updated on June 04, 2022

Comments

  • fabiangebert
    fabiangebert almost 2 years

    I want to split

    $path = getenv('PATH');
    

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

  • RaYell
    RaYell over 14 years
    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.
  • Matthew Scharley
    Matthew Scharley over 14 years
    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.
  • c089
    c089 over 14 years
    I think he means the separator for the Entries in the PATH environment variable (e.g. ":" on *nix), not the separator inside the paths.
  • mauris
    mauris over 11 years
    Note that this post was written in 2009. split() has been deprecated according to the PHP manual.
  • Pacerier
    Pacerier almost 9 years
    @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.