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'));
Comments
-
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 over 14 yearsYou cannot use
DIRECTORY_SEPARATOR
for that. You must usePATH_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 over 14 yearsinclude_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 over 14 yearsI think he means the separator for the Entries in the PATH environment variable (e.g. ":" on *nix), not the separator inside the paths.
-
mauris over 11 yearsNote that this post was written in 2009.
split()
has been deprecated according to the PHP manual. -
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.