This article mainly shares with you how to obtain the query information of the current page with php and js. I hope it can help you.
1.php Get the query information of the current page and store it in an associative array and return
function get(){ //得到当前页面URL的查询字符串 $query = $_SERVER['QUERY_STRING']; // 将每个键值对分开存储在数组中 $arr1 = explode('&', $query); foreach($arr1 as $val){ // 遍历每一个键值对将键与值相分离 $arr2 = explode('=', $val); $arr3[$arr2[0]] = $arr2[1]; } return $arr3; } echo '<pre class="brush:php;toolbar:false">'; print_r(get()); echo '';
2.JS Convert the parameter list of the current page into an object in the form
function get(){ //获取当前页面的查询字符串 var url = window.location.search.substring(1); //将每一个键值对分开存储在数组中 var arr1 = url.split('&'); //定义一个对象来存储转换结果 var obj = {}; // 遍历数组中的每个键值对将他们对应存储在对象中(按照键为属性名,值为属性值的形式) arr1.forEach(function(item){ //将每一个键值对的键与值相分离 var arr2 = item.split('='); obj[arr2[0]] = arr2[1]; }); return obj; } console.log(get());
The above is the detailed content of How to get the query information of the current page in php and js. For more information, please follow other related articles on the PHP Chinese website!