Login   Register  
PHP Classes

File: RelativePath.Example1.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Asbjorn Grandt  >  Relative Path  >  RelativePath.Example1.php  >  Download  
File: RelativePath.Example1.php
Role: Example script
Content type: text/plain
Description: Example and testing facility for RelativePath
Class: Relative Path
Clean file name paths removing redundant elements
Author: By
Last change: Code cleaning
Rev. 1.00 - 2011-03-13
* Fixed: Pasing Windows absolute paths would result in
relative paths where the Windows drive designation would be treated as
just another path element.
Date: 3 months ago
Size: 3,003 bytes


Class file image Download
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$docRoot $_SERVER["DOCUMENT_ROOT"] . "/";

if (!empty(
$_POST['path'])) {
$paths = array(stripslashes($_POST['path']));
} else {
// These paths are designed to be as insane as reasonably possible. Do not try to make much sense of them please :)
$paths = array(
$docRoot "/../../home/./John Doe/work/site/test/../../www/Project.1",
$docRoot "/../../../../../../../../home//./\\\\/John Doe/work\\site/test/../../www/Project.1",
"../../../home/./John Doe/work/site/test/../../www/Project.1/",
"../../../home/./John Doe/work/site/test/../../www/Project.1/" "/../Project.2/index.html",
"./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1" "/" "../Project.2/index.html",
"../../home/../../../John Doe/work/site/test/../../www/Project.1" "/" "../Project.2/index.html/../",
"/media/Projects/www/test/images/../../home/../../../John Doe/work/site/test.2/../../www/Project.1");

 * Time execution time for a function
 * @param unknown_type $function
 * @param unknown_type $title
 * @param unknown_type $iterations
function execTime($function$title=""$iterations 100000) {
$usec$sec) = explode(" "microtime());
$t1 = ($sec+$usec) * 1000;
    for (
$i $i $iterations $i++) {
$usec$sec) = explode(" "microtime());
$t2 = ($sec+$usec) * 1000;
$t2 $t2 $t1;
    if (!empty(
$title)) {
"<pre>$title: " number_format((double)$t22) . " ms.</pre>\n";
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8" />
<style type="text/css">
body,table,form {
    font-size: 10pt;
    font-family: verdana, helvetica, sans-serif;

dt {
    font-weight: bold;
    margin-bottom: 0px;
    padding-bottom: 0px;
    background-color: #eeeeee;

dd {
    margin-top: 0px;
    padding-top: 0px;
    margin-bottom: 2ex;

pre {
    margin: 0px;
    padding: 0px;
<title>Relative Path tests</title>
<h1>Relative Path tests</h1>
print "<p>docRoot: $docRoot</p>\n";
<form method="post">
<p>Path:<br />
<input type="text" size="120" name="path"
    value="<?php echo stripslashes($_POST['path']); ?>" /></p>
<p><input type="submit" /></p>
foreach ($paths as $path) {
"<dt><pre>Path '$path' becomes:</pre></dt>\n";
"'" RelativePath::getRelativePath($path) . "'\n";
// This test requires PHP 5.3, due to the use of an anonymous function.
//    execTime(function() {
//        RelativePath::getRelativePath("./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1/../Project.2/index.html");
//    }, "100,000 iterations took");