好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP 四种URL解析处理方式的例子 - php高级应用

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

在已知URL参数的情况下,我们可以根据自身情况采用$_GET来获取相应的参数信息($_GET[\\\'name\\\']);那在未知情况下如何获取到URL上的参数信息呢?

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

如果需要包含文件名的话可以使用$_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           )   )   */   ?> 

查看更多关于PHP 四种URL解析处理方式的例子 - php高级应用的详细内容...

  阅读:46次