如何从 Perl 中的相对 URL 找到完整 URL?
我是 Perl 新手,但想知道是否有人知道与以下 PHP 版本类似的脚本,该脚本运行得很好!
private function resolve_href ( $base, $href ) {
if (!$href)
return $base;
$rel_parsed = parse_url($href);
if (array_key_exists('scheme', $rel_parsed))
return $href;
$base_parsed = parse_url("$base ");
if (!array_key_exists('path', $base_parsed))
$base_parsed = parse_url("$base/ ");
if ($href{0} === "/")
$path = $href;
else
$path = dirname($base_parsed['path']) . "/$href";
$path = preg_replace('~/\./~', '/', $path);
$parts = array();
foreach ( explode('/', preg_replace('~/+~', '/', $path)) as $part ) {
if ($part === "..")
array_pop($parts);
elseif ($part!="")
$parts[] = $part;
}
$dir = ( ( array_key_exists('scheme', $base_parsed)) ? $base_parsed['scheme'] . '://' . $base_parsed['host'] : "" ) . "/" . implode("/", $parts);
return str_replace( "\/", '', $dir );
}
非常感谢任何帮助
I'm new to perl but was wondering if anyone know of a script that was similar to the following PHP version which works great!
private function resolve_href ( $base, $href ) {
if (!$href)
return $base;
$rel_parsed = parse_url($href);
if (array_key_exists('scheme', $rel_parsed))
return $href;
$base_parsed = parse_url("$base ");
if (!array_key_exists('path', $base_parsed))
$base_parsed = parse_url("$base/ ");
if ($href{0} === "/")
$path = $href;
else
$path = dirname($base_parsed['path']) . "/$href";
$path = preg_replace('~/\./~', '/', $path);
$parts = array();
foreach ( explode('/', preg_replace('~/+~', '/', $path)) as $part ) {
if ($part === "..")
array_pop($parts);
elseif ($part!="")
$parts[] = $part;
}
$dir = ( ( array_key_exists('scheme', $base_parsed)) ? $base_parsed['scheme'] . '://' . $base_parsed['host'] : "" ) . "/" . implode("/", $parts);
return str_replace( "\/", '', $dir );
}
Any help is much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 URI:
输出:
See URI:
Output: