PHP的parse_url反向-解析的URL [英] php parse_url reverse -- parsed url

查看:697
本文介绍了PHP的parse_url反向-解析的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从解析的网址中还原网址?

Is there a way to reverse the url from a parsed url?

$url = 'http://www.domain.com/dir/index.php?query=blabla#more_bla';
$parse = parse_url($url);
print_r($parse);
/*
array(
 'scheme'=>'http://',
 etc....
)
*/
$revere = reverse_url($parse); // probably does not exist but u get the point

echo $reverse;
//outputs:// "http://www.domain.com/dir/index.php?query=blabla#more_bla"

或者如果有一种方法可以验证缺少推荐网址部分的网址,例如

Or if there is a way validate a url that is missing part of its recommended urls e.g

www.mydomain.com

mydomain.com

应该全部返回 http://www.mydomain.com 或具有正确的子域

should all return http://www.mydomain.com or with correct sub domain

推荐答案

您应该可以

http_build_url($parse)

注意: http_build_url 仅可通过安装pecl_http获得.

NOTE: http_build_url is only available by installing pecl_http.

根据文档,它是专门为处理parse_url的输出而设计的.这两个函数都处理锚点,查询参数等,因此没有"$ url上未提及的其他属性".

According to the docs it's designed specifically to handle the output from parse_url. Both functions handle anchors, query params, etc so there are no "other properties not mentioned on the $url".

要在缺少http://时添加http://,请在解析前使用基本检查:

To add http:// when it's missing, use a basic check before parsing it:

if (strpos($url, "http://") != 0)
    $url = "http://$url";

这篇关于PHP的parse_url反向-解析的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆