문서의 URL을 다른 문서 (절대적이거나 상대적 일 수 있음)에 연결하고이 링크를 절대적으로 사용해야한다고 가정 해 봅시다.상대 URL을 절대 주소로 변환
function absolute_url($url,$parent_url){
$parent_url=parse_url($parent_url);
if(strcmp(substr($url,0,7),'http://')==0){
return $url;
}
elseif(strcmp(substr($url,0,1),'/')==0){
return $parent_url['scheme']."://".$parent_url['host'].$url;
}
else{
$path=$parent_url['path'];
$path=substr($path,0,strrpos($path,'/'));
return $parent_url['scheme']."://".$parent_url['host']."$path/".$url;
}
}
$parent_url='http://example.com/path/to/file/name.php?abc=abc';
echo absolute_url('name2.php',$parent_url)."\n";
// output http://example.com/path/to/file/name2.php
echo absolute_url('/name2.php',$parent_url)."\n";
// output http://example.com/name2.php
echo absolute_url('http://name2.php',$parent_url)."\n";
// output http://name2.php
코드는 잘 작동하지만 이러한 작동하지 않습니다 ../../path/to/file.php
로 더 경우가있을 수 있습니다 :
그래서 표준 함수 나 함수가 내 함수보다 잘 (더 보편적으로) 그렇게합니까?
나는 Google에 비슷한 질문 (one 및 two)을 시도했지만 찾고있는 것이 아닌 서버 경로 관련 솔루션처럼 보입니다.
'.. \ .. \ 경로 \는 파일 경로, URL이 아니라, 그래서 별도의 기능을 보증한다. – Steve
@Steve 그래서 'file'는 HTML 문서에서 작동하지 않습니까? –
브라우저에 따라 다를 수 있지만이를 보장하지는 않습니다. uri (링크에 넣는 내용)에는 슬래시를 사용해야합니다. 나는 어색하게하려고하지 않고, 파일 경로에 대해 이야기하는 곳을 생각했습니다. 예를 들어 phps와 함께 사용하는 경우 등이 있습니다. – Steve