Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs

怪我咯
Release: 2023-03-07 21:56:01
Original
2899 people have browsed it

PHP functions pathinfo(), parse_url() and basename(), these three are all functions for parsing URLs, but there are some differences. This article lists some examples, hoping that these examples will help everyone understand it easier. Let’s take a look at the usage methods and techniques of these three functions:

1. Use pathinfo to parse the URL

The pathinfo function is to obtain the path, directory or file name.

Example, the code is as follows

<?

$test = pathinfo("http://www.php.cn/index.php");

print_r($test);

?>
Copy after login

Code running results:

Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs

##2. Use parse_url() function to parse

The parse_url() function is used to parse URLs and return an associative array of parsed components. This function will not detect whether the URL is legal, but will only try to parse it correctly.

Example, the code is as follows

<?
$test = parse_url("http://www.php.cnt/index.php?name=tank&sex=1#top");
print_r($test);
?>
Copy after login

Code running result:

Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs

##3. Use basename() to parse

The basename() function returns the file name in the path

Instance, the code is as follows

<?
$test = basename("http://localhost/index.php?name=tank&sex=1#top");
echo $test;
?>
Copy after login

The code running result:

Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs

[Related article recommendations]:

1.

Detailed explanation of the php pathinfo() function to obtain file path information


2.

Detailed explanation of the usage of the php basename() function to obtain the file name

3.

Detailed explanation of the definition and usage of the php parse_url() function

The above is the detailed content of Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!