Mercurial > hg > webaudioevaluationtool
diff php/rel2abs.php @ 2249:30c012132427
Implementation for #21 on PHP
author | Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk> |
---|---|
date | Tue, 19 Apr 2016 14:58:40 +0100 |
parents | |
children | 464c6c6692d6 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/rel2abs.php Tue Apr 19 14:58:40 2016 +0100 @@ -0,0 +1,32 @@ +<?php + +//http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php +function rel2abs($rel, $base) +{ + /* return if already absolute URL */ + if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel; + + /* queries and anchors */ + if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel; + + /* parse base URL and convert to local variables: + $scheme, $host, $path */ + extract(parse_url($base)); + + /* remove non-directory element from path */ + $path = preg_replace('#/[^/]*$#', '', $path); + + /* destroy path if relative url points to root */ + if ($rel[0] == '/') $path = ''; + + /* dirty absolute URL */ + $abs = "$host$path/$rel"; + + /* replace '//' or '/./' or '/foo/../' with '/' */ + $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); + for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} + + /* absolute URL is ready! */ + return $scheme.'://'.$abs; +} +?> \ No newline at end of file