PHP将外部相对路径转换为绝对路径
我试图弄清楚如何将“外部相对路径”转换为绝对路径: 我真的很想要一个能够执行以下操作的函数:
$path = "/search?q=query";
$host = "http://google.com";
$abspath = reltoabs($host, $path);
并使 $abspath 等于 "http:// google.com/search?q=query" 另一个例子:
$path = "top.html";
$host = "www.example.com/documentation";
$abspath = reltoabs($host, $path);
$abspath 等于 "http://www.example.com/documentation/ top.html"
问题是它不能保证采用这种格式,并且它可能已经是绝对的,或者完全指向不同的主机,我不太确定如何解决这个问题。 谢谢。
I am trying to figure out how to convert an "external relative path" to an absolute one:
I'd really like a function that will do the following:
$path = "/search?q=query";
$host = "http://google.com";
$abspath = reltoabs($host, $path);
And have $abspath equal to "http://google.com/search?q=query"
Another example:
$path = "top.html";
$host = "www.example.com/documentation";
$abspath = reltoabs($host, $path);
And have $abspath equal to "http://www.example.com/documentation/top.html"
The problem is that it is not guaranteed to be in that format, and it could already be absolute, or be pointing to a different host entirely, and I'm not quite sure how to approach this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该尝试 PECL 函数 http_build_url
http://php.net/manual/en/function.http-构建 url.php
You should try the PECL function http_build_url
http://php.net/manual/en/function.http-build-url.php
因此存在三种情况:
示例代码(未经测试):
So there are three cases:
Example code (untested):
看来我已经解决了我自己的问题:
对于我的目的来说,这似乎工作得很好。
It appears I have solved my own problem:
Which seems to work pretty well, for my purposes.