[phpstudy]PHP 四种URL解析处理方式的例子

更新时间:2019-04-18    来源:邮件处理    手机版     字体:

【www.bbyears.com--邮件处理】

第一种、利用$_SERVER内置数组变量

相对较为原始的$_SERVER["QUERY_STRING"]来获取,URL的参数,通常使用这个变量返回的会是类似这样的数据:name=tank&sex=1

如果需要包含文件名的话可以使用$_SERVER["REQUEST_URI"](返回类似:/index.php?name=tank&sex=1)


第二种、利用pathinfo内置函数
01  02 $test = pathinfo("http://localhost/index.php");
03 print_r($test);
04 /*
05 结果如下
06 Array
07 (
08     [dirname] => http://localhost //url的路径
09     [basename] => index.php  //完整文件名
10     [extension] => php  //文件名后缀
11     [filename] => index //文件名
12 )
13 */
14 ?>


第三种、利用parse_url内置函数
01  02 $test = parse_url("http://localhost/index.php?name=tank&sex=1#top");
03 print_r($test);
04 /*
05 结果如下
06 Array
07 (
08     [scheme] => http //使用什么协议
09     [host] => localhost //主机名
10     [path] => /index.php //路径
11     [query] => name=tank&sex=1 // 所传的参数
12     [fragment] => top //后面根的锚点
13 )
14 */
15 ?>


第四种、利用basename内置函数
2 $test = basename("http://localhost/index.php?name=tank&sex=1#top");
3 echo $test;
4 /*
5 结果如下
6 index.php?name=tank&sex=1#top
7 */
8 ?>


另外,还有就是自己通过正则匹配的处理方式来获取需要的值了。这种方式较为精确,效率暂不考虑。。。

下面拓展实践下正则处理方式:
01  02 preg_match_all("/(\w+=\w+)(#\w+)?/i","http://localhost/index.php?name=tank&sex=1#top",$match);
03 print_r($match);
04 /*
05 结果如下
06 Array
07 (
08     [0] => Array
09         (
10             [0] => name=tank
11             [1] => sex=1#top
12         )
13     [1] => Array
14         (
15             [0] => name=tank
16             [1] => sex=1
17         )
18     [2] => Array
19         (
20             [0] =>
21             [1] => #top
22         )
23 )
24 */
25 ?>


路途漫漫...还有待继续挖掘...

本文来源:http://www.bbyears.com/jiaocheng/49529.html

热门标签

更多>>

本类排行