By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,350 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,350 IT Pros & Developers. It's quick & easy.

Combining an absolute and relative Url

P: n/a
Is there any function for combining an absolute and a relative URL to result
in an absolute URL?
Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate
to http://www.domain.com/images/1.jpeg.

I know WinInet for windows has a function that does this called
InternetCombineUrl, I just need the same function in PHP
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Joe Cybernet" <no@no.ie> wrote in message
news:O3****************@news.indigo.ie...
Is there any function for combining an absolute and a relative URL to result in an absolute URL?
Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to http://www.domain.com/images/1.jpeg.

I know WinInet for windows has a function that does this called
InternetCombineUrl, I just need the same function in PHP

strAbs = strDom & strURI

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2

P: n/a
"Joe Cybernet" <no@no.ie> wrote in message
news:O3****************@news.indigo.ie...
Is there any function for combining an absolute and a relative URL to result in an absolute URL?
Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to http://www.domain.com/images/1.jpeg.

I know WinInet for windows has a function that does this called
InternetCombineUrl, I just need the same function in PHP

oops, I thought I was in the comp.lang.basic.visual, direagard my last post

$absURL = $dom . $URI;

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #3

P: n/a
There's no built-in function that does that. Here's one that should join
there correctly:

function InternetCombineUrl($absolute, $relative) {
extract(parse_url($absolute));
if($relative{0} == '/') {
$cparts = array_filter(explode("/", $relative));
}
else {
$aparts = array_filter(explode("/", $path));
$rparts = array_filter(explode("/", $relative));
$cparts = array_merge($aparts, $rparts);
foreach($cparts as $i => $part) {
if($part == '.') {
$cparts[$i] = null;
}
if($part == '..') {
$cparts[$i - 1] = null;
$cparts[$i] = null;
}
}
$cparts = array_filter($cparts);
}
$path = implode("/", $cparts);
$url = "";
if($scheme) {
$url = "$scheme://";
}
if($user) {
$url .= "$user";
if($pass) {
$url .= ":$pass";
}
$url .= "@";
}
if($host) {
$url .= "$host/";
}
$url .= $path;
return $url;
}

echo InternetCombineUrl("http://www.domain.com", "../images/1.jpeg");

Uzytkownik "Joe Cybernet" <no@no.ie> napisal w wiadomosci
news:O3****************@news.indigo.ie...
Is there any function for combining an absolute and a relative URL to result in an absolute URL?
Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to http://www.domain.com/images/1.jpeg.

I know WinInet for windows has a function that does this called
InternetCombineUrl, I just need the same function in PHP

Jul 17 '05 #4

P: n/a
Joe Cybernet wrote:
Is there any function for combining an absolute and a relative URL to result
in an absolute URL?


As has already been said, there isn't a built-in function for
resolving relative URLs. However, you might follow the algorithm set
out in RFC2396 sec. 5.2. Watch out if you're using parse_url, as it
parses some relative URLs incorrectly.

--
Jock
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.