Home > php教程 > php手册 > body text

PHP解析URL并得到URL中的参数

WBOY
Release: 2016-06-06 19:53:11
Original
1260 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 PHP解析URL并得到URL中的参数 ?php //例举一个URL格式的字符串: $str //1 用parse_url解析URL,此处是$str $arr = parse_url($str); var_dump($arr); //2 将URL中的参数取出来放到数组里 $arr_

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  PHP解析URL并得到URL中的参数

  

  //例举一个URL格式的字符串:

  $str

  //1 用parse_url解析URL,此处是$str

  $arr = parse_url($str);

  var_dump($arr);

  //2 将URL中的参数取出来放到数组里

  $arr_query = convertUrlQuery($arr['query']);

  var_dump($arr_query);

  //3 将 参数数组 再变回 字符串形式的参数格式

  var_dump(getUrlQuery($arr_query));

  /**

  * Returns the url query as associative array

  * @param     string     query

  * @return     array     params

  */

  function convertUrlQuery($query)

  {

  $queryParts = explode('&', $query);

  $params = array();

  foreach ($queryParts as $param)

  {

  $item = explode('=', $param);

  $params[$item[0]] = $item[1];

  }

  return $params;

  }

  function getUrlQuery($array_query)

  {

  $tmp = array();

  foreach($array_query as $k=>$param)

  {

  $tmp[] = $k.'='.$param;

  }

  $params = implode('&',$tmp);

  return $params;

  }

  ?>

  输出结果:

  array (size=4)

  'scheme' => string 'http' (length=4)

  'host' => string 'test.com' (length=8)

  'path' => string '/testdir/index.php' (length=18)

  'query' => string 'param1=10¶m2=20¶m3=30¶m4=40¶m5=50¶m6=60' (length=59)

  array (size=6)

  'param1' => string '10' (length=2)

  'param2' => string '20' (length=2)

  'param3' => string '30' (length=2)

  'param4' => string '40' (length=2)

  'param5' => string '50' (length=2)

  'param6' => string '60' (length=2)

  string 'param1=10¶m2=20¶m3=30¶m4=40¶m5=50¶m6=60' (length=59)

PHP解析URL并得到URL中的参数

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template