Home  >  Article  >  Backend Development  >  How to get the query information of the current page in php and js

How to get the query information of the current page in php and js

小云云
小云云Original
2018-03-30 13:40:521685browse

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 &#39;<pre class="brush:php;toolbar:false">&#39;;
	print_r(get());
	echo &#39;
';

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(&#39;&&#39;);
    //定义一个对象来存储转换结果
    var obj = {};
    // 遍历数组中的每个键值对将他们对应存储在对象中(按照键为属性名,值为属性值的形式)
    arr1.forEach(function(item){
      //将每一个键值对的键与值相分离
      var arr2 = item.split(&#39;=&#39;);
      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!

Statement:
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