解释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