如何将PHP中的相对路径URL转换成绝对路径URL?
- 内容介绍
- 文章标签
- 相关推荐
本文共计161个文字,预计阅读时间需要1分钟。
php/** * 将一个URL转换为完整的URL * PHP将相对路径URL转换为绝对路径URL */function format_url($srcurl, $baseurl) { $srcinfo=parse_url($srcurl); if (isset($srcinfo['scheme'])) { return $srcurl; } $baseinfo=parse_url($baseurl); $url=$baseinfo['scheme'] . '://'; if (isset($baseinfo['host'])) { $url .=$baseinfo['host']; } if (isset($baseinfo['port'])) { $url .=':' . $baseinfo['port']; } if (isset($baseinfo['path'])) { $url .=$baseinfo['path']; } $url .='/' . ltrim($srcinfo['path'], '/'); return $url;}
/** * 将一个URL转换为完整URL * PHP将相对路径URL转换为绝对路径URL */ function format_url($srcurl, $baseurl) { $srcinfo = parse_url($srcurl); if(isset($srcinfo['scheme'])) { return $srcurl; } $baseinfo = parse_url($baseurl); $url = $baseinfo['scheme'].'://'.$baseinfo['host']; if(substr($srcinfo['path'], 0, 1) == '/') { $path = $srcinfo['path']; }else{ $filename= basename($baseinfo['path']); //兼容基础url是列表 if(strpos($filename,".")===false){ $path = dirname($baseinfo['path']).'/'.$filename.'/'.$srcinfo['path']; }else{ $path = dirname($baseinfo['path']).'/'.$srcinfo['path']; } } $rst = array(); $path_array = explode('/', $path); if(!$path_array[0]) { $rst[] = ''; } foreach ($path_array AS $key => $dir) { if ($dir == '..') { if (end($rst) == '..') { $rst[] = '..'; }elseif(!array_pop($rst)) { $rst[] = '..'; } }elseif($dir && $dir != '.') { $rst[] = $dir; } } if(!end($path_array)) { $rst[] = ''; } $url .= implode('/', $rst); return str_replace('\\', '/', $url); }
本文共计161个文字,预计阅读时间需要1分钟。
php/** * 将一个URL转换为完整的URL * PHP将相对路径URL转换为绝对路径URL */function format_url($srcurl, $baseurl) { $srcinfo=parse_url($srcurl); if (isset($srcinfo['scheme'])) { return $srcurl; } $baseinfo=parse_url($baseurl); $url=$baseinfo['scheme'] . '://'; if (isset($baseinfo['host'])) { $url .=$baseinfo['host']; } if (isset($baseinfo['port'])) { $url .=':' . $baseinfo['port']; } if (isset($baseinfo['path'])) { $url .=$baseinfo['path']; } $url .='/' . ltrim($srcinfo['path'], '/'); return $url;}
/** * 将一个URL转换为完整URL * PHP将相对路径URL转换为绝对路径URL */ function format_url($srcurl, $baseurl) { $srcinfo = parse_url($srcurl); if(isset($srcinfo['scheme'])) { return $srcurl; } $baseinfo = parse_url($baseurl); $url = $baseinfo['scheme'].'://'.$baseinfo['host']; if(substr($srcinfo['path'], 0, 1) == '/') { $path = $srcinfo['path']; }else{ $filename= basename($baseinfo['path']); //兼容基础url是列表 if(strpos($filename,".")===false){ $path = dirname($baseinfo['path']).'/'.$filename.'/'.$srcinfo['path']; }else{ $path = dirname($baseinfo['path']).'/'.$srcinfo['path']; } } $rst = array(); $path_array = explode('/', $path); if(!$path_array[0]) { $rst[] = ''; } foreach ($path_array AS $key => $dir) { if ($dir == '..') { if (end($rst) == '..') { $rst[] = '..'; }elseif(!array_pop($rst)) { $rst[] = '..'; } }elseif($dir && $dir != '.') { $rst[] = $dir; } } if(!end($path_array)) { $rst[] = ''; } $url .= implode('/', $rst); return str_replace('\\', '/', $url); }

