parse_url() - php 网址URL函数
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_SCHEME
、PHP_URL_HOST
、PHP_URL_PORT
、PHP_URL_USER
、PHP_URL_PASS
、PHP_URL_PATH
、PHP_URL_QUERY
或PHP_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
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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!