当前位置:首页 > PHP教程 > php应用 > 列表

php如何解析url?解析url的5种方式介绍

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-12 09:13:54 浏览: 评论:0 

php解析url的几种方式

1、利用$_SERVER内置数组变量

访问:http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1

//URL的参数

echo $_SERVER['QUERY_STRING'];

返回:

m=admin&c=index&a=lists&catid=1&page=1

//包含文件名

echo $_SERVER["REQUEST_URI"];

返回:

/test.php?m=admin&c=index&a=lists&catid=1&page=1

2、利用pathinfo内置函数

  1. echo "<pre>"; 
  2.  
  3. $url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; 
  4.  
  5. var_export(pathinfo($url)); 

返回:

  1. array ( 
  2.  
  3.   'dirname' => 'http://localhost', 
  4.  
  5.   'basename' => 'test.php?m=admin&c=index&a=lists&catid=1&page=1#top', 
  6.  
  7.   'extension' => 'php?m=admin&c=index&a=lists&catid=1&page=1#top', 
  8.  
  9.   'filename' => 'test', 
  10.  
  11. ) 

3、利用parse_url内置函数

  1. echo "<pre>"; 
  2.  
  3. $url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; 
  4.  
  5. var_export(parse_url($url)); 

返回:

  1. array ( 
  2.  
  3.   'scheme' => 'http', 
  4.  
  5.   'host' => 'localhost', 
  6.  
  7.   'path' => '/test.php', 
  8.  
  9.   'query' => 'm=admin&c=index&a=lists&catid=1&page=1', 
  10.  
  11.   'fragment' => 'top', 
  12.  
  13. ) 

4、利用basename内置函数

  1. echo "<pre>"; 
  2.  
  3. $url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; 
  4.  
  5. var_export(basename($url)); 

返回:

test.php?m=admin&c=index&a=lists&catid=1&page=1#top

5、正则匹配

  1. echo "<pre>"; 
  2.  
  3. $url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; 
  4.  
  5. preg_match_all("/(\w+=\w+)(#\w+)?/i",$url,$match); 
  6.  
  7. var_export($match); 

返回:

  1. array ( 
  2.  
  3.   0 =>  
  4.  
  5.   array ( 
  6.  
  7.     0 => 'm=admin', 
  8.  
  9.     1 => 'c=index', 
  10.  
  11.     2 => 'a=lists', 
  12.  
  13.     3 => 'catid=1', 
  14.  
  15.     4 => 'page=1#top', 
  16.  
  17.   ), 
  18.  
  19.   1 =>  
  20.  
  21.   array ( 
  22.  
  23.     0 => 'm=admin', 
  24.  
  25.     1 => 'c=index', 
  26.  
  27.     2 => 'a=lists', 
  28.  
  29.     3 => 'catid=1', 
  30.  
  31.     4 => 'page=1', 
  32.  
  33.   ), 
  34.  
  35.   2 =>  
  36.  
  37.   array ( 
  38.  
  39.     0 => '', 
  40.  
  41.     1 => '', 
  42.  
  43.     2 => '', 
  44.  
  45.     3 => '', 
  46.  
  47.     4 => '#top', 
  48.  
  49.   ), 
  50.  
  51. ) 

url常用处理方法

  1. /** 
  2.  
  3.  * 将字符串参数变为数组 
  4.  
  5.  * @param $query 
  6.  
  7.  * @return array 
  8.  
  9.  */ 
  10.  
  11. function convertUrlQuery($query) 
  12.  
  13. { 
  14.  
  15.     $queryParts = explode('&', $query); 
  16.  
  17.     $params = array(); 
  18.  
  19.     foreach ($queryParts as $param) { 
  20.  
  21.         $item = explode('=', $param); 
  22.  
  23.         $params[$item[0]] = $item[1]; 
  24.  
  25.     } 
  26.  
  27.     return $params; 
  28.  
  29. } 
  30.  
  31.  
  32.  
  33. /** 
  34.  
  35.  * 将参数变为字符串 
  36.  
  37.  * @param $array_query 
  38.  
  39.  * @return string 
  40.  
  41.  */ 
  42.  
  43. function getUrlQuery($array_query) 
  44.  
  45. { 
  46.  
  47.     $tmp = array(); 
  48.  
  49.     foreach ($array_query as $k => $param) { 
  50.  
  51.         $tmp[] = $k . '=' . $param; 
  52.  
  53.     } 
  54.  
  55.     $params = implode('&', $tmp); 
  56.  
  57.     return $params; 
  58.  
  59. } 

例:

  1. echo "<pre>"; 
  2.  
  3. $url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; 
  4.  
  5. $arr = parse_url($url); 
  6.  
  7. $arr_query = convertUrlQuery($arr['query']); 
  8.  
  9. var_export($arr_query); 

返回:

  1. array ( 
  2.  
  3.   'm' => 'admin', 
  4.  
  5.   'c' => 'index', 
  6.  
  7.   'a' => 'lists', 
  8.  
  9.   'catid' => '1', 
  10.  
  11.   'page' => '1', 
  12.  
  13. ) 
  14.  
  15.  
  16. var_export(getUrlQuery($arr_query)); 

返回:

m=admin&c=index&a=lists&catid=1&page=1

Tags: php如何解析url

分享到: