PHP 四种URL解析处理方式的例子
URL parsing
Processing method
在已知URL参数的情况下,我们可以根据自身情况采用$_GET来获取相应的参数信息($_GET[\'name\']);那在未知情况下如何获取到URL上的参数信息呢?
第一种:利用$_SERVER内置数组变量
相对较为原始的$_SERVER['QUERY_STRING']来获取,URL的参数,通常使用这个变量返回的会是类似这样的数据:name=tank&sex=1
如果需要包含文件名的话可以使用$_SERVER["REQUEST_URI"](返回类似:/index.php?name=tank&sex=1)
第二种:利用pathinfo内置函数
<?php $test = pathinfo("http://localhost/index.php"); print_r($test); /* 结果如下 Array ( [dirname] => http://localhost //url的路径 [basename] => index.php //完整文件名 [extension] => php //文件名后缀 [filename] => index //文件名 ) */ ?>
Copy after login
第三种:利用parse_url内置函数
<?php $test = parse_url("http://localhost/index.php?name=tank&sex=1#top"); print_r($test); /* 结果如下 Array ( [scheme] => http //使用什么协议 [host] => localhost //主机名 [path] => /index.php //路径 [query] => name=tank&sex=1 // 所传的参数 [fragment] => top //后面根的锚点 ) */ ?>
Copy after login
第四种:利用basename内置函数
<?php $test = basename("http://localhost/index.php?name=tank&sex=1#top"); echo $test; /* 结果如下 index.php?name=tank&sex=1#top */ ?>
Copy after login
另外,还有就是自己通过正则匹配的处理方式来获取需要的值了,这种方式较为精确,效率暂不考虑,下面拓展实践下正则处理方式:
<?php preg_match_all("/(\w+=\w+)(#\w+)?/i", "http://localhost/index.php?name=tank&sex=1#top", $match); print_r($match); /* 结果如下 Array ( [0] => Array ( [0] => name=tank [1] => sex=1#top ) [1] => Array ( [0] => name=tank [1] => sex=1 ) [2] => Array ( [0] => [1] => #top ) ) */
Copy after login
文章链接:
随便收藏,请保留本文地址!
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
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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Where to find the Crane Control Keycard in Atomfall
1 weeks ago
By DDD

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
CakePHP Tutorial
1359
52

