PathParser Documentation

PathParser is a PHP class intended to handle all kinds of path: relative, absolute and URLs. It also resolves references to '/./', '/../' and extra '/' characters in the input path, and it can find the relative path between to paths. It works with virtual files (what means that the file does NOT need to exist).


Property Type Description
$pathstringParsed path.
$rootstringPath root. If exists, should be something like "", "C:/" or "/".
$dirstringPath after root (only directories).
$filestringFile name, if path doesn't end on dir.
$extensionstringFile extension.
$schemestringIf path is an URL, returns something like "http", "ftp".
$hoststringEx. "".
$portintegerHost port.
$querystringstringQuery string (part after the "?").
$fragmentstringFragment (part after the "#").


void PathParser ( [string $path] )

Class constructor. Can define the class property $path in the optional parameter. It calls parse() method.

array parse ( [string $path] )

Main method. Parses the path indicated by $path and stores the results on each property. It resolves all the './', '../' and extras '/' before parse the path. It'll store in the class property $path the resolved path, and will also return an associative array with each path part (each key has the same name of the corresponding class property).

string fix ( [string $path] )

This method is an emulation of PHP's native realpath(), with to basic differences. First, you MAY use virtual files. It will resolve all the './', '../' and extra '/' even if the file does not exists. Second, it will return the path in the same style of the original entry, not always absolute, as in realpath(). So, if you input an URL, it will return the resolved path as an URL.

string findRelativePath ( string $path_A, string $path_B )

This method returns the relative path from $path_A to $path_B. Note that the 'root' part of these paths must be exactly the same (could be both empty), so it can find the relative path between them.

array parseQueryString ( [string $querystring] )

This method returns and associative array with the values in $querystring (the parameter should be in 'query string' format). Ex. var1=value1&var2=value2 would return array('var1' => 'value1', 'var2' => 'value2').



// Imports PathParser class
require "includes/class.pathparser.php";

$path = "";

// Starts an object
$Path = new PathParser($path);

/*  At this point:

$Path->path        = "";
$Path->root        = "";
$Path->dir         = "manual/en/";
$Path->file        = "install.php";
$Path->extension   = "php";
$Path->scheme      = "http";
$Path->host        = "";
$Path->port        = "80";
$Path->user        = "";
$Path->pass        = "";
$Path->querystring = "version=502";
$Path->fragment    = "top";


$path_A = "";
$path_B = "";

echo $Path->findRelativePath($path_A, $path_B) . "<br />";
echo $Path->findRelativePath($path_B, $path_A);

/*  This would print:





Carlos Reche
carlosreche at yahoo dot com

Dez 19, 2004
Sorocaba, SP - Brazil