Visita il nostro notiziario web - Le migliori notizie da tutto il mondo NotiziarioWeb.it

getpath (Note: this is not a core php function, it is my own defined function(see below), but it is in "manual format" in order to give a better and faster understanding of the function itself.

(PHP 3, PHP 4 )

getpath -- return part of a translated path

Description

string getpath ([int which_part[, bool relative]])

Returns part of a translated path, or the whole path, excluding the $SCRIPT_NAME

If which_part is positive the returned string will have part(s) or all of path translated starting from left.
If which_part is negative the returned string will have part(s) or all of path translated starting from right.

If relative is omitted the returned string will have all of path translated starting from left or right depending on the signum od which_start.
If relative is specified the returned string will have only a single part of path translated starting from left or right depending on the signum of which_start.

Note: getpath returns path separated by doubled slashes because the slash character is a special charachter, hence in strings it must be escaped.

Note: See below for UNIX/Linux getpath version.

Examples and usage:

--->Path translated = "F:\www\fake\demo\getpath\getpath.php3"


Omitting the "relative" flag path parts are returned as absolute

$part = getpath(); // returns "F:\\www\\fake\\demo\\getpath\\"

$part = getpath(0); // returns "F:\\"

$part = getpath(1); // returns "F:\\www\\"

$part = getpath(2); // returns "F:\\www\\fake\\"

$part = getpath(-1); // returns "F:\\www\\fake\\demo\\getpath\\"

$part = getpath(-2); // returns "F:\\www\\fake\\demo\\"

$part = getpath(-3); // returns "F:\\www\\fake\\"


Activating the "relative" flag path parts are returned as relative

$part = getpath(1,1); // returns "www\\"

$part = getpath(2,1); // returns "fake\\"

$part = getpath(3,1); // returns "demo\\"

$part = getpath(-1,1); // returns "getpath\\"

$part = getpath(-2,1); // returns "demo\\"

$part = getpath(-3,1); // returns "fake\\"





And here's the code:

Note: This function is designed for WIN OS, but it can easily be adjusted for Unix/Linux by simply changing the $sep variable inside the function and the double slash in the explode call.($sep = "\\\\"; for WIN, $sep = "/" and $arrpath = explode("/", $pt); for UNIX/Linux)

  function getpath($which = 1000, $relative = "") {
// getpath function by Nicola Delbono key5@key5.com
global $PATH_TRANSLATED;
$sep = "\\\\";
$pt = "$PATH_TRANSLATED";
$arrpath = explode("\\", $pt);
$pathct = count($arrpath);

	// $which
	if ($which >= $pathct ) {
	$which = $pathct - 2;
	}
	elseif ($which < 0)
	{
	$which = $pathct - 1 + $which ;
	}
	elseif ($which == 1000)
	{
	$which = $pathct - 2;
	}

	// $relative
	if($relative == 1)
	{
		$path = $arrpath[$which] . $sep;
	}
	elseif ($relative == 0){
		for ($r = 0 ;$r <= $which ; ++$r) {
		$path .= $arrpath[$r] . $sep;
		}
	}
return $path;
}