在已知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 //文件名 ) */ ?>
第三种:利用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 //后面根的锚点 ) */ ?>
第四种:利用basename内置函数
<?php $test = basename("http://localhost/index.php?name=tank&sex=1#top"); echo $test; /* 结果如下 index.php?name=tank&sex=1#top */ ?>
另外,还有就是自己通过正则匹配的处理方式来获取需要的值了,这种方式较为精确,效率暂不考虑,下面拓展实践下正则处理方式:
<?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 ) ) */
文章链接:
随便收藏,请保留本文地址!

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

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),