#50 expandlinks with relative URI

open
nobody
None
5
2005-07-28
2005-07-28
Anonymous
No

Hi there,

I had some problems with relative URI like
"../../path/show.htm" and the expandlink function. So I
decide to to code a new function for expanding URLs.
Here is what I programm. It works fine for me. Please
let me know whether it is good enough for you too
schneider@ng-marketing.com.

function _expandlinks($links,$URI)
{

$parsed = parse_url($URI);

# complete URL
if (substr($links,0,4)=='http') {
return $links;
}

# absolute URI
if (substr($links,0,1)=='/') {
$uri = $parsed['scheme'] ?
$parsed['scheme'].':'.((strtolower($parsed['scheme'])
== 'mailto') ? '':'//'): '';
$uri .= $parsed['user'] ?
$parsed['user'].($parsed['pass']?
':'.$parsed['pass']:'').'@':'';
$uri .= $parsed['host'] ? $parsed['host'] : '';
$uri .= $parsed['port'] ?
':'.$parsed['port'] : '';
$uri .= links;
return $uri;
}

# relative URI
if (substr($links,0,1)=='.') {
$uri = $parsed['scheme'] ?
$parsed['scheme'].':'.((strtolower($parsed['scheme'])
== 'mailto') ? '':'//'): '';
$uri .= $parsed['user'] ?
$parsed['user'].($parsed['pass']?
':'.$parsed['pass']:'').'@':'';
$uri .= $parsed['host'] ? $parsed['host'] : '';
$uri .= $parsed['port'] ?
':'.$parsed['port'] : '';
$uri .= $parsed['path'] ? $parsed['path'] : '';
$uri .= $parsed['query'] ?
'?'.$parsed['query'] : '';
$uri .= $parsed['fragment'] ?
'#'.$parsed['fragment'] : '';
$uri .= '/'.$links;
return $uri;
}

# else it is a path relative URI
$uri = $parsed['scheme'] ?
$parsed['scheme'].':'.((strtolower($parsed['scheme'])
== 'mailto') ? '':'//'): '';
$uri .= $parsed['user'] ?
$parsed['user'].($parsed['pass']?
':'.$parsed['pass']:'').'@':'';
$uri .= $parsed['host'] ? $parsed['host'] : '';
$uri .= $parsed['port'] ? ':'.$parsed['port'] : '';
$uri .= $parsed['path'] ? $parsed['path'] : '';
if ($parsed['path'])
$uri .=
substr($uri,0,strrpos($uri,'/')+1).$links;
else
$uri .= '/'.$links;
return $uri;
}

Discussion