Forum Moderators: coopster
I would like to know if i am approaching this wrong (which i think i am).
I would like to take an absolute path ie: c:/path/to/files/ and use a function to read the path and check between the "/" to see how many dir's are in the filetree.
I am thinking i can use ereg or ereg_replace to check for patterns between the '/' and then count the amount of '/' returned but i think that i have got the syntax wrong:-
ereg_replace("^/[a-z]¦[0-9]/$","/",$path);
Any help is greatly appreciated - this is just a curiosity for the most part though.
thank you,
MRB
explode [php.net] and count [php.net] would probably be quickest and easiest. It will also allow you to play around with the path "parts" a bit if necessary. An example you can use to learn ...
$paths = array(
'path',
'C:/this/is/the/path',
'C:/this/is/the/path/',
'/this/is/the/path',
'/this/is/the/path/'
);
print '<pre>';
foreach ($paths as $path) {
$p = explode('/', $path);
print count($p) . ': ' . htmlentities($path) . "\n";
$p = array_values(array_filter($p));
print count($p) . ': ' . htmlentities(implode('/', $p)) . "\n";
if (preg_match('/^[a-z]:/i', $p[0])) {
unset($p[0]);
print count($p) . ': ' . htmlentities(implode('/', $p)) . "\n";
}
print "\n";
}
print '</pre>';
exit;
(here)/this/is/the/path/(and here)
The array_values [php.net] is present merely to reset the numerical sequence of the array after filtering out empty indexes.
Finally, the regular expression is looking for any Windows OS root. Any first path (array index zero) that starts with a letter followed by a colon, case-insensitive.
Wow, thanks for the help. I have figured out what i want to get out of this now, basically, it involves filepaths as you have guessed. My intention is to write a function that takes two parameters, firstly a constant value (the path the the root directory) and then either '+1' or '-1' the amount of directorys in the file tree. Hopefully this will negate the need for using '../' everytime i want to access a folder.
My apologies for going on a bit - hopefully i have made myself a little clearer ;-)
Cheers,
MRB