Home > Article > Backend Development > php uses the functions pathinfo(), parse_url() and basename() to parse the URL_php instance
This article mainly introduces the example code of using PHP functions pathinfo()
, parse_url()
and basename()
to parse URLs. Not much to say below, let’s look at the code directly
The example code is as follows:
1. Use pathinfo to parse URL
<? $test = pathinfo("http://localhost/index.php"); print_r($test); ?>
The results are as follows
Array ( [dirname] => http://localhost //url的路径 [basename] => index.php //完整文件名 [extension] => php //文件名后缀 [filename] => index //文件名 )
2. Use parse_url() function to parse
<? $test = parse_url("http://localhost/index.php?name=tank&sex=1#top"); print_r($test); ?>
The results are as follows
Array ( [scheme] => http //使用什么协议 [host] => localhost //主机名 [path] => /index.php //路径 [query] => name=tank&sex=1 // 所传的参数 [fragment] => top //后面根的锚点 )
3. Use basename() to parse
<? $test = basename("http://localhost/index.php?name=tank&sex=1#top"); echo $test; ?>
The results are as follows
index.php?name=tank&sex=1#top
Summary
The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.