使用PHP将相对path转换为绝对URL

如何使用PHP,将相对path转换为绝对URL?

function rel2abs($rel, $base) { /* return if already absolute URL */ if (parse_url($rel, PHP_URL_SCHEME) != '') 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; } 

我喜欢jordanstephens从链接提供的代码! 我投了票。 l0oky启发了我,以确保该function是端口,用户名和密码的URL兼容。 我需要它为我的项目。

 function rel2abs( $rel, $base ) { /* return if already absolute URL */ if( parse_url($rel, PHP_URL_SCHEME) != '' ) 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 = ''; /* do we have a user in our URL? */ if( isset($user) ) { $abs.= $user; /* password too? */ if( isset($pass) ) $abs.= ':'.$pass; $abs.= '@'; } $abs.= $host; /* did somebody sneak in a port? */ if( isset($port) ) $abs.= ':'.$port; $abs.=$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 ); } 

添加了支持以保持当前查询。 帮助很多?page = 1等等…

 function rel2abs($rel, $base) { /* return if already absolute URL */ if (parse_url($rel, PHP_URL_SCHEME) != '') return ($rel); /* queries and anchors */ if ($rel[0] == '#' || $rel[0] == '?') return ($base . $rel); /* parse base URL and convert to local variables: $scheme, $host, $path, $query, $port, $user, $pass */ 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 = ''; /* do we have a user in our URL? */ if (isset($user)) { $abs .= $user; /* password too? */ if (isset($pass)) $abs .= ':' . $pass; $abs .= '@'; } $abs .= $host; /* did somebody sneak in a port? */ if (isset($port)) $abs .= ':' . $port; $abs .= $path . '/' . $rel . (isset($query) ? '?' . $query : ''); /* replace '//' or '/./' or '/foo/../' with '/' */ $re = ['#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#']; for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) { } /* absolute URL is ready! */ return ($scheme . '://' . $abs); } 

实际上不是关于转换path而不是url的问题? PHP实际上有一个这样的function: realpath() 。 唯一应该注意的是符号链接。

PHP手册中的示例:

 chdir('/var/www/'); echo realpath('./../../etc/passwd') . PHP_EOL; // Prints: /etc/passwd echo realpath('/tmp/') . PHP_EOL; // Prints: /tmp 

如果相对目录已经存在,这将做这个工作:

 function rel2abs($relPath, $baseDir = './') { if ('' == trim($path)) { return $baseDir; } $currentDir = getcwd(); chdir($baseDir); $path = realpath($path); chdir($currentDir); return $path; } 

这个函数将相对URLparsing为$pgurl 给定的当前页面url, 而不使用正则expression式 。 它成功地解决了:

/home.php?exampletypes,

same-dir nextpage.phptypes,

../...../.../parentdirtypes,

完整的http://example.neturl,

和简写//example.neturl

 //Current base URL (you can dynamically retrieve from $_SERVER) $pgurl = 'http://example.com/scripts/php/absurl.php'; function absurl($url) { global $pgurl; if(strpos($url,'://')) return $url; //already absolute if(substr($url,0,2)=='//') return 'http:'.$url; //shorthand scheme if($url[0]=='/') return parse_url($pgurl,PHP_URL_SCHEME).'://'.parse_url($pgurl,PHP_URL_HOST).$url; //just add domain if(strpos($pgurl,'/',9)===false) $pgurl .= '/'; //add slash to domain if needed return substr($pgurl,0,strrpos($pgurl,'/')+1).$url; //for relative links, gets current directory and appends new filename } function nodots($path) { //Resolve dot dot slashes, no regex! $arr1 = explode('/',$path); $arr2 = array(); foreach($arr1 as $seg) { switch($seg) { case '.': break; case '..': array_pop($arr2); break; case '...': array_pop($arr2); array_pop($arr2); break; case '....': array_pop($arr2); array_pop($arr2); array_pop($arr2); break; case '.....': array_pop($arr2); array_pop($arr2); array_pop($arr2); array_pop($arr2); break; default: $arr2[] = $seg; } } return implode('/',$arr2); } 

用法示例:

 echo nodots(absurl('../index.html')); 

必须在URL转换为绝对调用nodots()

点函数是多余的,但可读,快速,不使用正则expression式,并将解决99%的典型url(如果你想100%确定,扩展开关块,以支持6 +点,虽然我从来没有在URL中看到过很多点)。

希望这可以帮助,

我使用了相同的代码: http : //nashruddin.com/PHP_Script_for_Converting_Relative_to_Absolute_URL,但我修改了一点点,所以如果基地址包含端口号,它返回相对的URL与端口号。

 function rel2abs($rel, $base) { /* return if already absolute URL */ if (parse_url($rel, PHP_URL_SCHEME) != '') 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 // with port number if exists */ if (parse_url($base, PHP_URL_PORT) != ''){ $abs = "$host:".parse_url($base, PHP_URL_PORT)."$path/$rel"; }else{ $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; } 

希望这可以帮助别人!