parse_url用来解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分
PHP 解析 URL函数: parse_url详解
parse_url — 解析 URL,返回其组成部分
说明
array parse_url ( string $url )
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。
本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url() 会尝试尽量正确地将其解析。
参数
url
要解析的 URL
返回值
对严重不合格的 URL,parse_url() 可能会返回 FALSE 并发出 E_WARNING。否则会返回一个关联数组,其组成部分为(至少有一个):
scheme – 如 http
host
port
user
pass
path
query – 在问号 ? 之后
fragment – 在散列符号 # 之后
范例
parse_url() 例子
代码如下:
<?php $url = 'http://username:password@hostname/path?arg=value#anchor'; print_r(parse_url($url)); ?>
以上例程会输出:
Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
常常我们会传送一大串网址,然后要解析的话,只能不断地去切token来解析。但是在php中,一定要学会使用前人所写过的函式,这样开发速度才会快别人一截。当然,要解析网址的话,就得去找相关的指令,而今天要介绍的,就是php中的parse_url这个函式。
这个函式主要是用来解析网址,而首先先看一下这个函式原型,其官方描述如下:
mixed parse_url ( string $url [, int $component= -1 ] )
而php官方举了一个例子,而我将他扩充如下,以方便讲解。
代码如下:
<?php $url = 'http://username:password@hostname/path?arg1=value1&arg2=value2#anchor'; print_r(parse_url($url)); ?>
首先,先看一下这个程式的输出结果
Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg1=value1&arg2=value2 [fragment] => anchor )
其中,scheme是指他走的路线,而host则是站台网址,user则是使用者名称,pass则是密码,path则是路径,query则是参数,fragment则是锚点。
而在「$component」的部份,则是有以下的参数可供传递。
PHP_URL_SCHEME
PHP_URL_HOST
PHP_URL_USER
PHP_URL_PASS
PHP_URL_PATH
PHP_URL_QUERY
PHP_URL_FRAGMENT
而这几个参数则分别代表了Array中的scheme、host、user、pass、path、query和fragment。
同样,举例来说,如果使用PHP_URL_PATH和PHP_URL_QUERY来说,以下为他的示范。
代码如下:
<?php $url = 'http://username:password@hostname/path?arg1=value1&arg2=value2#anchor'; echo nl2br(parse_url($url, PHP_URL_PATH)."\n"); echo nl2br(parse_url($url, PHP_URL_QUERY)."\n"); ?>
而输出结果将如下:
/path
arg1=value1&arg2=value2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
