PHP URL参数与数组相互转化笔记

4 篇文章 1 订阅
解释URL
<?php

$url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BB%81&oq=sdf2&rsv_pq=eac5e1b600030b31&rsv_t=59eaDy7I4dUKhjRunVROavgszccF0eEVbrBU37WDAqhC%2FcdLH95WLfsuIUg&rqlang=cn&rsv_enter=1&inputT=12382&rsv_sug3=19&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_sug4=12382';
$info = parse_url($url);
parse_str($info['query'],$url_params_arr);
var_dump($info,$url_params_arr);
exit;

输出结果

array(4) {
  ["scheme"]=>
  string(5) "https"
  ["host"]=>
  string(13) "www.baidu.com"
  ["path"]=>
  string(2) "/s"
  ["query"]=>
  string(251) "ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BB%81&oq=sdf2&rsv_pq=eac5e1b600030b31&rsv_t=59eaDy7I4dUKhjRunVROavgszccF0eEVbrBU37WDAqhC%2FcdLH95WLfsuIUg&rqlang=cn&rsv_enter=1&inputT=12382&rsv_sug3=19&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_sug4=12382"
}
array(17) {
  ["ie"]=>
  string(5) "utf-8"
  ["f"]=>
  string(1) "8"
  ["rsv_bp"]=>
  string(1) "1"
  ["rsv_idx"]=>
  string(1) "1"
  ["tn"]=>
  string(5) "baidu"
  ["wd"]=>
  string(3) "仁"
  ["oq"]=>
  string(4) "sdf2"
  ["rsv_pq"]=>
  string(16) "eac5e1b600030b31"
  ["rsv_t"]=>
  string(59) "59eaDy7I4dUKhjRunVROavgszccF0eEVbrBU37WDAqhC/cdLH95WLfsuIUg"
  ["rqlang"]=>
  string(2) "cn"
  ["rsv_enter"]=>
  string(1) "1"
  ["inputT"]=>
  string(5) "12382"
  ["rsv_sug3"]=>
  string(2) "19"
  ["rsv_sug1"]=>
  string(2) "15"
  ["rsv_sug7"]=>
  string(3) "100"
  ["rsv_sug2"]=>
  string(1) "0"
  ["rsv_sug4"]=>
  string(5) "12382"
}
转化回GET参数
<?php

$url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BB%81&oq=sdf2&rsv_pq=eac5e1b600030b31&rsv_t=59eaDy7I4dUKhjRunVROavgszccF0eEVbrBU37WDAqhC%2FcdLH95WLfsuIUg&rqlang=cn&rsv_enter=1&inputT=12382&rsv_sug3=19&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_sug4=12382';
$info = parse_url($url);
parse_str($info['query'],$url_params_arr);
//var_dump($info,$url_params_arr);

$query = http_build_query($url_params_arr);
echo $query;
exit;

输出结果

ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BB%81&oq=sdf2&rsv_pq=eac5e1b600030b31&rsv_t=59eaDy7I4dUKhjRunVROavgszccF0eEVbrBU37WDAqhC%2FcdLH95WLfsuIUg&rqlang=cn&rsv_enter=1&inputT=12382&rsv_sug3=19&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_sug4=12382

主要使用函数:

parse_url
parse_str
http_build_query
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值