百科狗-知识改变命运!
--

parse_url() - php 网址URL函数

乐乐1年前 (2023-11-21)阅读数 11#技术干货
文章标签路径

parse_url()

(PHP 4, PHP 5, PHP 7)

解析 URL,返回其组成部分

说明

parse_url(string $url[,int $component=-1]): mixed

本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。

本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url()会尝试尽量正确地将其解析。

参数

$url

要解析的 URL。无效字符将使用_来替换。

$component

指定PHP_URL_SCHEMEPHP_URL_HOSTPHP_URL_PORTPHP_URL_USERPHP_URL_PASSPHP_URL_PATHPHP_URL_QUERYPHP_URL_FRAGMENT的其中一个来获取 URL 中指定的部分的string。(除了指定为PHP_URL_PORT后,将返回一个integer的值)。

返回值

对严重不合格的 URL,parse_url()可能会返回FALSE

如果省略了$component参数,将返回一个关联数组array,在目前至少会有一个元素在该数组中。数组中可能的键有以下几种:

  • scheme-如http
  • host
  • port
  • user
  • pass
  • path
  • query-在问号?之后
  • fragment-在散列符号#之后

如果指定了$component参数,parse_url()返回一个string(或在指定为PHP_URL_PORT时返回一个integer)而不是array。如果 URL 中指定的组成部分不存在,将会返回NULL

更新日志

版本说明
5.4.7修复了host协议省略时的识别。
5.3.3在 URL 解析失败时将不会产生E_WARNING级别的错误。
5.1.2增加了参数$component。

范例

Example #1parse_url()例子

以上例程会输出:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

parse_url() - php 网址URL函数

Example #2parse_url()解析丢失协议的例子

以上例程会输出:

array(3) {
  ["host"]=>
  string(15) "www.example.com"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(17) "googleguy=googley"
}

注释

Note:

本函数不能用于相对 URL。Note:

parse_url()是专门用来解析 URL 而不是 URI 的。不过为遵从 PHP 向后兼容的需要有个例外,对 file://协议允许三个斜线(file:///...)。其它任何协议都不能这样。

参见

  • pathinfo() 返回文件路径的信息
  • parse_str() 将字符串解析成多个变量
  • http_build_query() 生成 URL-encode 之后的请求字符串
  • http_build_url()
  • dirname() 返回路径中的目录部分
  • basename() 返回路径中的文件名部分
  • » RFC 3986
[If you haven't yet] been able to find a simple conversion back to string from a parsed url, here's an example: 
It may be worth reminding that the value of the #fragment never gets sent to the server. Anchors processing is exclusively client-side.
Here is utf-8 compatible parse_url() replacement function based on "laszlo dot janszky at gmail dot com" work. Original incorrectly handled URLs with user:pass. Also made PHP 5.5 compatible (got rid of now deprecated regex /e modifier). 
I was writing unit tests and needed to cause this function to kick out an error and return FALSE in order to test a specific execution path. If anyone else needs to force a failure, the following inputs will work: 
Created another parse_url utf-8 compatible function. 
Here's a good way to using parse_url () gets the youtube link.
This function I used in many works:

parse_url () allocates a unique youtube code and put into iframe link and displayed on your page. The size of the videos choose yourself.
Enjoy.
Based on the idea of "jbr at ya-right dot com" have I been working on a new function to parse the url:

This returns:
Array
(
  [0] => me:you@sub.site.org:29000/pear/validate.html?happy=me&sad=you#url
  [scheme] => 
  [1] => 
  [login] => me
  [2] => me
  [pass] => you
  [3] => you
  [host] => sub.site.org
  [4] => sub.site.org
  [subdomain] => sub
  [5] => sub
  [domain] => site.org
  [6] => site.org
  [extension] => org
  [7] => org
  [port] => 29000
  [8] => 29000
  [path] => /pear/validate.html
  [9] => /pear/validate.html
  [file] => validate.html
  [10] => validate.html
  [arg] => happy=me&sad=you
  [11] => happy=me&sad=you
  [anchor] => url
  [12] => url
)
So both named and numbered array keys are possible.
It's quite advanced, but I think it works in any case... Let me know if it doesn't...
I have coded a function which converts relative URL to absolute URL for a project of mine. Considering I could not find it elsewhere, I figured I would post it here.
The following function takes in 2 parameters, the first parameter is the URL you want to convert from relative to absolute, and the second parameter is a sample of the absolute URL.
Currently it does not resolve '../' in the URL, only because I do not need it. Most webservers will resolve this for you. If you want it to resolve the '../' in the path, it just takes minor modifications.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)