PHP中解析URL的五种方法分别有哪些?

更新于
2026-09-29 00:42:00
2阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计695个文字,预计阅读时间需要3分钟。

PHP中解析URL的五种方法分别有哪些?

PHP解析URL的几种方式:

1.使用$_SERVER内置数组变量

PHP中解析URL的五种方法分别有哪些?

2.使用parse_url()函数

3.使用explode()函数

示例代码:

php

php解析url的几种方式

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

访问:
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内置函数

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

返回:

array ( 'dirname' => 'localhost', 'basename' => 'test.php?m=admin&c=index&a=lists&catid=1&page=1#top', 'extension' => 'php?m=admin&c=index&a=lists&catid=1&page=1#top', 'filename' => 'test', )

3、利用parse_url内置函数

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

返回:

array ( 'scheme' => 'localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; var_export(basename($url));

返回:

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

5、正则匹配

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

返回:

array ( 0 => array ( 0 => 'm=admin', 1 => 'c=index', 2 => 'a=lists', 3 => 'catid=1', 4 => 'page=1#top', ), 1 => array ( 0 => 'm=admin', 1 => 'c=index', 2 => 'a=lists', 3 => 'catid=1', 4 => 'page=1', ), 2 => array ( 0 => '', 1 => '', 2 => '', 3 => '', 4 => '#top', ), )

url常用处理方法

/** * 将字符串参数变为数组 * @param $query * @return array */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; } /** * 将参数变为字符串 * @param $array_query * @return string */ function getUrlQuery($array_query) { $tmp = array(); foreach ($array_query as $k => $param) { $tmp[] = $k . '=' . $param; } $params = implode('&', $tmp); return $params; }

例:

echo "<pre>"; $url = 'localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; $arr = parse_url($url); $arr_query = convertUrlQuery($arr['query']); var_export($arr_query);

返回:

array ( 'm' => 'admin', 'c' => 'index', 'a' => 'lists', 'catid' => '1', 'page' => '1', )

var_export(getUrlQuery($arr_query));

返回:

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

相关教程

本文共计695个文字,预计阅读时间需要3分钟。

PHP中解析URL的五种方法分别有哪些?

PHP解析URL的几种方式:

1.使用$_SERVER内置数组变量

PHP中解析URL的五种方法分别有哪些?

2.使用parse_url()函数

3.使用explode()函数

示例代码:

php

php解析url的几种方式

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

访问:
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内置函数

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

返回:

array ( 'dirname' => 'localhost', 'basename' => 'test.php?m=admin&c=index&a=lists&catid=1&page=1#top', 'extension' => 'php?m=admin&c=index&a=lists&catid=1&page=1#top', 'filename' => 'test', )

3、利用parse_url内置函数

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

返回:

array ( 'scheme' => 'localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; var_export(basename($url));

返回:

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

5、正则匹配

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

返回:

array ( 0 => array ( 0 => 'm=admin', 1 => 'c=index', 2 => 'a=lists', 3 => 'catid=1', 4 => 'page=1#top', ), 1 => array ( 0 => 'm=admin', 1 => 'c=index', 2 => 'a=lists', 3 => 'catid=1', 4 => 'page=1', ), 2 => array ( 0 => '', 1 => '', 2 => '', 3 => '', 4 => '#top', ), )

url常用处理方法

/** * 将字符串参数变为数组 * @param $query * @return array */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; } /** * 将参数变为字符串 * @param $array_query * @return string */ function getUrlQuery($array_query) { $tmp = array(); foreach ($array_query as $k => $param) { $tmp[] = $k . '=' . $param; } $params = implode('&', $tmp); return $params; }

例:

echo "<pre>"; $url = 'localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top'; $arr = parse_url($url); $arr_query = convertUrlQuery($arr['query']); var_export($arr_query);

返回:

array ( 'm' => 'admin', 'c' => 'index', 'a' => 'lists', 'catid' => '1', 'page' => '1', )

var_export(getUrlQuery($arr_query));

返回:

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

相关教程