欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP URL參數(shù)獲取方式的四種例子

 更新時(shí)間:2014年02月28日 09:41:00   作者:  
這篇文章主要介紹了PHP URL參數(shù)獲取方式的四種例子,php url參數(shù)解析的4種方法,需要的朋友可以參考下

在已知URL參數(shù)的情況下,我們可以根據(jù)自身情況采用$_GET來獲取相應(yīng)的參數(shù)信息($_GET['name']);那,在未知情況下如何獲取到URL上的參數(shù)信息呢?

第一種、利用$_SERVER內(nèi)置數(shù)組變量

相對較為原始的$_SERVER['QUERY_STRING']來獲取,URL的參數(shù),通常使用這個(gè)變量返回的會(huì)是類似這樣的數(shù)據(jù):name=tank&sex=1
如果需要包含文件名的話可以使用$_SERVER["REQUEST_URI"](返回類似:/index.php?name=tank&sex=1)

第二種、利用pathinfo內(nèi)置函數(shù)


復(fù)制代碼 代碼如下:

<?php
$test = pathinfo("http://localhost/index.php");
print_r($test);
/*
結(jié)果如下
Array
(
     [dirname] => http://localhost //url的路徑
     [basename] => index.php  //完整文件名
     [extension] => php  //文件名后綴
     [filename] => index //文件名
)
*/
?>

第三種、利用parse_url內(nèi)置函數(shù)


復(fù)制代碼 代碼如下:

<?php
$test = parse_url("http://localhost/index.php?name=tank&sex=1#top");
print_r($test);
/*
結(jié)果如下
Array
(
     [scheme] => http //使用什么協(xié)議
     [host] => localhost //主機(jī)名
     [path] => /index.php //路徑
     [query] => name=tank&sex=1 // 所傳的參數(shù)
     [fragment] => top //后面根的錨點(diǎn)
)
*/
?>

第四種、利用basename內(nèi)置函數(shù)


復(fù)制代碼 代碼如下:

<?php
$test = basename("http://localhost/index.php?name=tank&sex=1#top");
echo $test;
/*
結(jié)果如下
index.php?name=tank&sex=1#top
*/
?>

另外,還有就是自己通過正則匹配的處理方式來獲取需要的值了。這種方式較為精確,效率暫不考慮。。。
下面拓展實(shí)踐下正則處理方式:

復(fù)制代碼 代碼如下:

<?php
preg_match_all("/(\w+=\w+)(#\w+)?/i","http://localhost/index.php?name=tank&sex=1#top",$match);
print_r($match);
/*
結(jié)果如下
Array
(
    [0] => Array
        (
            [0] => name=tank
            [1] => sex=1#top
        )
    [1] => Array
         (
            [0] => name=tank
             [1] => sex=1
         )
     [2] => Array
        (
             [0] =>
            [1] => #top
        )
)
*/
?>


路途漫漫...還有待繼續(xù)挖掘...

相關(guān)文章

最新評論