I know that you have already accepted the answer, but there is a very simple one-line solution to this problem that I use regularly, and I feel that it should be posted here:
$pathParts = explode('/', rtrim(str_replace('\\', '/', $path)));
This replaces the backslash with a slash, truncates any slashes, and explodes. This can be done safely, since Windows paths cannot contain forward slashes, and linux paths cannot contain backslashes.
The resulting array does not look like the one you described above - the root of the path will not contain a slash - but in fact it is best represented in this way. This is because the root of the path (i.e. C:\ or '/') is actually not useful when saving with a slash. As a result of this, you can call implode('/', $pathParts); and get the right way back, while with your array you get an extra slash in the root. In addition, \Users\User\My Documents (without a drive letter) is still a valid path on Windows, it just implies the current working volume.
source share