note 29484 added to function.dirname
| From: | jon at rack1 dot php dot net | Date: | Fri, 14 Feb 2003 19:22:34 +0000 |
| Subject: | note 29484 added to function.dirname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43953@lists.php.net to get a copy of this message | ||
Here's a lil' code I cooked up to figure out the base directories, as far as filesystem
vs. URLs. This is very helpful for, say an image that you want to use getimagesize(); on, but only
have the URL oriented directory. You can then use str_replace(); or preg_replace(); and swap the
values out easily!
Note that in order for it to work, you must include the pointer ($big = &$y), rather than simply
copying ($big = $y).
// separate each dir into array elements
$x = explode('/', dirname($_SERVER['SCRIPT_FILENAME']));
$y = explode('/', dirname($_SERVER['SCRIPT_NAME']));
$countX = count($x);
$countY = count($y);
$max = max($countX, $countY);
$min = min($countX, $countY);
$dif = $max-$min;
if (isset($x[$max-1])) { // $x is the biggest
$big = &$x; // make $big a pointer to $x
$lil = &$y; // make $lil a pointer to $y
} else { // $y is the biggest
$big = &$y; // make $big a pointer to $y
$lil = &$x; // make $lil a pointer to $x
}
for($i=$max-1;$i>=$dif; $i--) {
if ($big[$i]==$lil[$i-$dif]) { // path matches
unset($big[$i], $lil[$i-$dif]); // erase any common elements
}
}
// all that's left is uncommon elements, join array(s)
$root_dir = join('/', $x).'/';
$root_url = join('/', $y).'/';
--
http://www.php.net/manual/en/function.dirname.php
http://master.php.net/manage/user-notes.php?action=edit+29484
http://master.php.net/manage/user-notes.php?action=delete+29484
http://master.php.net/manage/user-notes.php?action=reject+29484