- function formaturl($url, $str){
- if (is_array($str)) {
- $return = array();
- foreach ($str as $href) {
- $return[] = formaturl($url, $href);
- }
- return $return;
- } else {
- if (stripos($str, 'http://')===0 || stripos($str, 'ftp://')===0) {
- return $str;
- }
- $str = str_replace('\\\\', '/', $str);
- $parseUrl = parse_url(dirname($url).'/');
- $scheme = isset($parseUrl['scheme']) ? $parseUrl['scheme'] : 'http';
- $host = $parseUrl['host'];
- $path = isset($parseUrl['path']) ? $parseUrl['path'] : '';
- $port = isset($parseUrl['port']) ? $parseUrl['port'] : '';
- if (strpos($str, '/')===0) {
- return $scheme.'://'.$host.$str;
- } else {
- $part = explode('/', $path);
- array_shift($part);
- $count = substr_count($str, '../');
- if ($count>0) {
- for ($i=0; $i<=$count; $i++) {
- array_pop($part);
- }
- }
- $path = implode('/', $part);
- $str = str_replace(array('../','./'), '', $str);
- $path = $path=='' ? '/' : '/'.trim($path,'/').'/';
- return $scheme.'://'.$host.$path.$str;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/101220137913.html
来源: http://www.codesnippet.cn/detail/101220137913.html