url is a unified resource locator, a concise representation of the location and access method of resources available from the Internet. It is a standard resource on the Internet. the address of. This article will introduce how to use PHP functions to obtain the parameters of each part of the URL.
parse_url()
This function can parse a URL and return its components. It is used as follows:
array parse_url(string $url)
This function returns an associative array containing the various components of the existing URL. If one of these components is missing, no array entry will be created for this component. The components are:
- scheme - such as http
- host - such as localhost
- port - such as 80
- user
- pass
- path - such as /parse_str.php
- query - after the question mark? such as id=1&category=php&title=php-install
- fragment - after the hash symbol
#This function does not mean that the given URL is valid, it just separates the parts in the list above. parse_url() accepts incomplete URLs and tries to parse them correctly. This function has no effect on relative path URLs.
<?php $url = "http://52php.cnblogs.com/welcome/"; $parts = parse_url($url); print_r($parts); ?>
The program running results are as follows:
Array ( [scheme] => http [host] => 52php.cnblogs.com [path] => /welcome/ )
<?php $url = 'http://username:password@hostname/path?arg=value#anchor'; print_r(parse_url($url)); echo '<br />'; echo parse_url($url, PHP_URL_PATH); ?>
Program output:
Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
As you can see, it is easy to decompose the various parts of a URL. It is also easy to extract the specified parts, such as:
echo parse_url($url, PHP_URL_PATH);
is in the second Among the parameters, set the following parameters: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT.
parse_str()
parse_str is used to parse the query string in the URL, that is, the string value that can be obtained through $_SERVER['QUERY_STRING'] , if the URL we request is http://localhost/parse_str.php?id=1&category=php&title=php-install, then the value returned by $_SERVER['QUERY_STRING'] is id=1&category=php&title=php-install, and This form of string happens to be parsed into an associative array using parse_str.
Usage is as follows:
void parse_str(string $str [, array &$arr ])
This function receives two parameters, $str is the string that needs to be parsed , and the optional parameter $arr is the variable name where the array value generated after parsing is stored. If you ignore the optional parameter, you can directly call variables like $id, $category, and $title. The script below simulates a GET request.
<?php <a href="http://localhost/parse_str.php?id=1&category=php&title=php-install">Click Here</a> $query_str = $_SERVER['QUERY_STRING']; parse_str($query_str); /* 这种方式可以直接使用变量$id, $category, $title */ parse_str($query_str, $query_arr); ?> <pre class="brush:php;toolbar:false"><?php print_r($query_arr); ?>?> /* 运行结果 */ Array ( [id] => 1 [category] => php [title] => php-install ) 1 php php-install
http_build_query Is to convert an array into a url? The following parameter string will be automatically urlencoded
string http_build_query (array formdata [, string numeric_prefix])
Add subscripts to the array if there is no specified key or the key is a number
[Recommended course: PHP video tutorial 】
The above is the detailed content of php gets the parameters of each part of the URL. For more information, please follow other related articles on the PHP Chinese website!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
