php中使用parse_url()對網址進行解析的實現(xiàn)代碼(parse_url詳解)
Jun 05, 2017 am 10:58 AMparse_url用來解析一個 URL 并返回一個關聯(lián)數(shù)組,包含在 URL 中出現(xiàn)的各種組成部分
PHP 解析 URL函數(shù): parse_url詳解
parse_url — 解析 URL,返回其組成部分
說明
array parse_url ( string $url )
本函數(shù)解析一個 URL 并返回一個關聯(lián)數(shù)組,包含在 URL 中出現(xiàn)的各種組成部分。
本函數(shù)不是用來驗證給定 URL 的合法性的,只是將其分解為下面列出的部分。不完整的 URL 也被接受,parse_url() 會嘗試盡量正確地將其解析。
參數(shù)
url
要解析的 URL
返回值
對嚴重不合格的 URL,parse_url() 可能會返回 FALSE 并發(fā)出 E_WARNING。否則會返回一個關聯(lián)數(shù)組,其組成部分為(至少有一個):
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中,一定要學會使用前人所寫過的函式,這樣開發(fā)速度才會快別人一截。當然,要解析網址的話,就得去找相關的指令,而今天要介紹的,就是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則是參數(shù),fragment則是錨點。
而在「$component」的部份,則是有以下的參數(shù)可供傳遞。
PHP_URL_SCHEME
PHP_URL_HOST
PHP_URL_USER
PHP_URL_PASS
PHP_URL_PATH
PHP_URL_QUERY
PHP_URL_FRAGMENT
而這幾個參數(shù)則分別代表了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

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If you want to check some relevant information in Steam or share some website links with friends, how do you do it? Can I enter a URL into steam? Of course it is possible. Below I will teach you how to open a URL in Steam. Specific methods: 1. First, we open steam. 2. Click the Settings option. 3. Then switch to the interface tab. 4. Check Show address bar. 5. Then enter the URL in the address bar to use the browser.

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Detailed analysis and examples of exponential functions in C language Introduction: The exponential function is a common mathematical function, and there are corresponding exponential function library functions that can be used in C language. This article will analyze in detail the use of exponential functions in C language, including function prototypes, parameters, return values, etc.; and give specific code examples so that readers can better understand and use exponential functions. Text: The exponential function library function math.h in C language contains many functions related to exponentials, the most commonly used of which is the exp function. The prototype of exp function is as follows
