PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of P Guardiario   PHP URI   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Sample output
Class: PHP URI
Convert relative to absolute URLs
Author: By
Last change:
Date: 12 years ago
Size: 3,277 bytes
 

Contents

Class file image Download
<?php

require('../rel2abs.php');
require(
'../url_to_absolute.php');
require(
'phpuri.php');

$tests=array(
  array(
'rel' => 'g:h', 'result' => 'g:h'),
  array(
'rel' => 'g', 'result' => 'http://a/b/c/g'),
  array(
'rel' => './g', 'result' => 'http://a/b/c/g'),
  array(
'rel' => 'g/', 'result' => 'http://a/b/c/g/'),
  array(
'rel' => '/g', 'result' => 'http://a/g'),
  array(
'rel' => '//g', 'result' => 'http://g'),
  array(
'rel' => 'g?y', 'result' => 'http://a/b/c/g?y'),
  array(
'rel' => '#s', 'result' => 'http://a/b/c/d;p?q#s'),
  array(
'rel' => 'g#s', 'result' => 'http://a/b/c/g#s'),
  array(
'rel' => 'g?y#s', 'result' => 'http://a/b/c/g?y#s'),
  array(
'rel' => ';x', 'result' => 'http://a/b/c/;x'),
  array(
'rel' => 'g;x', 'result' => 'http://a/b/c/g;x'),
  array(
'rel' => 'g;x?y#s', 'result' => 'http://a/b/c/g;x?y#s'),
  array(
'rel' => '.', 'result' => 'http://a/b/c/'),
  array(
'rel' => './', 'result' => 'http://a/b/c/'),
  array(
'rel' => '..', 'result' => 'http://a/b/'),
  array(
'rel' => '../', 'result' => 'http://a/b/'),
  array(
'rel' => '../g', 'result' => 'http://a/b/g'),
  array(
'rel' => '../..', 'result' => 'http://a/'),
  array(
'rel' => '../../', 'result' => 'http://a/'),
  array(
'rel' =>'../../g','result' =>'http://a/g'),
  array(
'rel' =>'g.','result' =>'http://a/b/c/g.'),
  array(
'rel' =>'.g','result' =>'http://a/b/c/.g'),
  array(
'rel' =>'g..','result' =>'http://a/b/c/g..'),
  array(
'rel' =>'..g','result' =>'http://a/b/c/..g'),
  array(
'rel' =>'./../g','result' =>'http://a/b/g'),
  array(
'rel' =>'./g/.','result' =>'http://a/b/c/g/'),
  array(
'rel' =>'g/./h','result' =>'http://a/b/c/g/h'),
  array(
'rel' =>'g/../h','result' =>'http://a/b/c/h'),
  array(
'rel' =>'g;x=1/./y','result' =>'http://a/b/c/g;x=1/y'),
  array(
'rel' =>'g;x=1/../y','result' =>'http://a/b/c/y'),
  array(
'rel' =>'g?y/./x','result' =>'http://a/b/c/g?y/./x'),
  array(
'rel' =>'g?y/../x','result' =>'http://a/b/c/g?y/../x'),
  array(
'rel' =>'g#s/./x','result' =>'http://a/b/c/g#s/./x'),
  array(
'rel' =>'g#s/../x','result' =>'http://a/b/c/g#s/../x')
);

# rel2abs
$start = microtime();
$base = 'http://a/b/c/d;p?q';
list(
$successes, $failures) = array(0,0);
foreach(
$tests as $test){
  if((
$r = rel2abs($test['rel'], $base)) == $test['result']){
   
$successes++;
  } else {
   
$failures++;
  }
}

$elapsed = microtime() - $start;
echo
"rel2abs: successes -> $successes, failures => $failures, elapsed time: $elapsed\n";

# url_to_absolute
$start = microtime();
$base = 'http://a/b/c/d;p?q';
list(
$successes, $failures) = array(0,0);
foreach(
$tests as $test){
  if((
$r = url_to_absolute($base, $test['rel'])) == $test['result']){
   
$successes++;
  } else {
   
$failures++;
  }
}

$elapsed = microtime() - $start;
echo
"url_to_absolute: successes -> $successes, failures => $failures, elapsed time: $elapsed\n";

# phpuri
$start = microtime();
$base = phpUri::parse('http://a/b/c/d;p?q');
list(
$successes, $failures) = array(0,0);
foreach(
$tests as $test){
  if((
$r = $base->join($test['rel'])) == $test['result']){
   
$successes++;
  } else {
   
$failures++;
    echo
"failure: $r instead of " . $test['result'] . " \n";
  }
}
$elapsed = microtime() - $start;
echo
"phpuri: successes -> $successes, failures => $failures, elapsed time: $elapsed\n";
?>