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).

Properties


Property Type Description
$pathstringParsed path.
$rootstringPath root. If exists, should be something like "http://www.something.com", "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. "www.something.com".
$portintegerHost port.
$userstringUsername.
$passstringPassword.
$querystringstringQuery string (part after the "?").
$fragmentstringFragment (part after the "#").

Methods


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').

Example


<?php

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


$path = "http://www.php.net:80/donwloads/..//manual/en/./install.php?version=502#top";

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


/*  At this point:

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

*/



$path_A = "http://www.php.net/manual/en/install.php";
$path_B = "http://www.php.net/downloads";

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

/*  This would print:

../../downloads/
../manual/en/install.php

*/

?>

Credits


Carlos Reche
carlosreche at yahoo dot com

Dez 19, 2004
Sorocaba, SP - Brazil