Home Backend Development PHP Tutorial Url几个惯用的函数

Url几个惯用的函数

Jun 13, 2016 pm 12:26 PM
page parse query string url

Url几个常用的函数

parse_url()

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

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

    <span style="color: #800080;">$_url</span> = "http://www.baidu.com/web?id=15&page=5"<span style="color: #000000;">;    $_par = pares_url($_url);    </span><span style="color: #008080;">var_dump</span>($_par);<br><br>输出结果:
Copy after login
<span style="color: #0000ff;">array</span>(4<span style="color: #000000;">) {  [</span>"scheme"]=>  <span style="color: #0000ff;">string</span>(4) "http"<span style="color: #000000;">  [</span>"host"]=>  <span style="color: #0000ff;">string</span>(13) "www.baidu.com"<span style="color: #000000;">  [</span>"path"]=>  <span style="color: #0000ff;">string</span>(4) "/web"<span style="color: #000000;">  [</span>"query"]=>  <span style="color: #0000ff;">string</span>(12) "id=15&page=5"<span style="color: #000000;">}</span>
Copy after login
Copy after login

parse_str()

将字符串解析成多个变量

    <span style="color: #008080;">parse_str</span>(<span style="color: #800080;">$_par</span>['query'],<span style="color: #800080;">$_query</span><span style="color: #000000;">);    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$_query</span><span style="color: #000000;">);输出结果:</span><span style="color: #0000ff;">array</span>(2<span style="color: #000000;">) {  [</span>"id"]=>  <span style="color: #0000ff;">string</span>(2) "15"<span style="color: #000000;">  [</span>"page"]=>  <span style="color: #0000ff;">string</span>(1) "5"<span style="color: #000000;">}</span>
Copy after login

 

http_bulid_query()

使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。

    <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$_query</span>['page']);  <span style="color: #008000;">//</span><span style="color: #008000;">清空page</span>    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">http_build_query</span>(<span style="color: #800080;">$_query</span><span style="color: #000000;">));输出:</span><span style="color: #0000ff;">string</span>(5) "id=15"
Copy after login

 

用于解析、拆分、重组URL字符串。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP function introduction—get_headers(): Get the response header information of the URL PHP function introduction—get_headers(): Get the response header information of the URL Jul 25, 2023 am 09:05 AM

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

What is the difference between html and url What is the difference between html and url Mar 06, 2024 pm 03:06 PM

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

How to get your Steam ID in a few steps? How to get your Steam ID in a few steps? May 08, 2023 pm 11:43 PM

Nowadays, many Windows users who love games have entered the Steam client and can search, download and play any good games. However, many users' profiles may have the exact same name, making it difficult to find a profile or even link a Steam profile to other third-party accounts or join Steam forums to share content. The profile is assigned a unique 17-digit id, which remains the same and cannot be changed by the user at any time, whereas the username or custom URL can. Regardless, some users don't know their Steamid, and it's important to know this. If you don't know how to find your account's Steamid, don't panic. In this article

How to use URL encoding and decoding in Java How to use URL encoding and decoding in Java May 08, 2023 pm 05:46 PM

Use url to encode and decode the class java.net.URLDecoder.decode(url, decoding format) decoder.decoding method for encoding and decoding. Convert into an ordinary string, URLEncoder.decode(url, encoding format) turns the ordinary string into a string in the specified format packagecom.zixue.springbootmybatis.test;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.net. URLEncoder

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

See all articles