dtihe8614 2010-08-18 02:28
浏览 74

如何从URL中提取参数并使用PHP将每个参数存储在一个新变量中[复制]

Possible Duplicate:
How to grab URL parameters using PHP?

I was wondering how can I store each url parameter each in there own new variable when I don't know how deep the url parameters are?

Here is an example of a URL value.

http://www.localhost.com/topics/index.php?cat=3&sub1=sub-1&sub2=sub-2&sub3=sub-3&sub4=sub-4

Here is my PHP script.

 $url = $_SERVER['QUERY_STRING'];
 $query = array();

 if(!empty($url)){
   foreach(explode('&', $url) as $part){
   list($key, $value) = explode('=', $part, 2);
   $query[$key] = $value;
  }
 }
  • 写回答

5条回答

  • dougaicha5258 2010-08-18 02:32
    关注

    PHP has this functionality already built in.

    $query = parse_str($url);
    extract($query)
    
    // example
    if(isset($sub1)) {
      echo $sub1;
    }
    

    see parse_str for more information

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32f103c8t6采集HLK-LD2450毫米波雷达传感器数据并打印显示距离和角度
  • ¥15 不重复生成字母+数字交易号
  • ¥15 AD封装导入后层混乱了
  • ¥15 如何将excel中的数据导入ansys中
  • ¥15 MATLAB中图像问题
  • ¥15 rk3399 安卓7.1 应用音频无声音
  • ¥20 Chatgpt被封号以后,换用apple和gmail注册失败,直接跳转封号界面
  • ¥15 这几个有点疑问解答一下
  • ¥15 r语言如何绘制这样的降水地图
  • ¥15 服务器一块硬盘重启过程中安装后,硬盘不识别了怎么修复?