URL补全函数

function spliturl ($url, $length) {
$url_array = explode (’/', $url);
$url_array_length = count ($url_array);
if ($url_array_length - 3 < $length) {
$length = $url_array_length - 3;
}

switch ($length) {
case 0: {
$split_url = $url_array[2];
break;
}

case 2: {
for ($i = 2; $i < $url_array_length - 2; ++$i) {
$split_url .= $url_array[$i] . ‘/’;
}

break;
}

case 1: {
for ($i = 2; $i < $url_array_length - 1; ++$i) {
$split_url .= $url_array[$i] . ‘/’;
}

break;
}

default: {
for ($i = 2; $i < $url_array_length - $length; ++$i) {
$split_url .= $url_array[$i] . ‘/’;
}
}
}

return ‘http://’ . $split_url;
}

function changepath ($url, $link) {
$ps_first = substr ($link, 0, 1);
if (strstr ($link, ‘://’)) {
return $link;
} else {
if ($ps_first == ‘/’) {
$remote_path = spliturl ($url, 0);
$url_1[] = $remote_path;
return $remote_path . $link;
} else {
if ($ps_first == ‘.’) {
$ps_array = explode (’/', $link);
$ps_lg = strpos ($link, ‘/’);
if ($ps_lg <= 3) {
if ($ps_array[0] == ‘.’) {
$remote_path = spliturl ($url, 1);
$url_1[] = $remote_path;
$value1 = substr ($link, $ps_lg - 1);
return $remote_path . $value1;
} else {
$remote_path = spliturl ($url, 2);
$url_1[] = $remote_path;
$value1 = substr ($link, $ps_lg + 1);
if (strstr ($value1, ‘../’)) {
return changepath ($remote_path, $value1);
} else {
return $remote_path . $value1;
}
}
} else {
return null;
}
} else {
if ($ps_first == ‘?’) {
$urlArray = parse_url ($url);
$url = str_replace (’?’ . $urlArray['query'], ”, $url);
$url .= $link;
return $url;
} else {
$remote_path = spliturl ($url, 1);
$url_1[] = $remote_path;
return $remote_path . $link;
}
}
}
}
}