Getting the parameter value, given the parameter name, is easy to do with regular expressions.
JS implementation method is as follows:
The code is as follows |
Copy Code |
function GetValue (URL, name) { var reg = new RegExp (' (\?| &) ' + name + ' = ([^&?] *) ', ' I '; var arr = Url.match (reg); if (arr) { return arr[2]; } return null; } |
Or
Method One: Regular Analysis method
The code is as follows |
Copy Code |
function getquerystring (name) { var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); var r = window.location.search.substr (1). Match (REG); if (r!= null) return unescape (r[2]); return null; } This call: Alert (getquerystring ("parameter name 1")); Alert (getquerystring ("parameter Name 2")); Alert (getquerystring ("parameter name 3")); |
If you want to get all the parameter names and their corresponding values, you can also use the method of the regular expression.
JS Implementation method:
The code is as follows |
Copy Code |
function Getkeyvalue (URL) { var result = {}; var reg = new RegExp (' ([\?| and]) (. +?) = ([^&?] *) ', ' IG '; var arr = reg.exec (URL); while (arr) { RESULT[ARR[2]] = arr[3]; arr = reg.exec (URL); } return result; } |
Note that there is also a method for matching in JS called Match,match is a string, and exec is the method of RegExp object. Using the match method of a string, while the regular expression is specified as a global match, the grouping in the regular expression is not used and the result returned is a substring of all matching regular expressions. When the Exec method does not use a global match flag, the first matched substring of the time returned, if the global match flag is used, the first time a string that matches the symbol from scratch is invoked again, starting with the last match.
Here's how to implement PHP:
The code is as follows |
Copy Code |
function Getkeyvalue ($url) { $result = Array (); $MR = Preg_match_all ('/? | &) (. +?) = ([^&?] *)/I ', $url, $matchs); if ($mr!== FALSE) { for ($i = 0; $i < $MR; $i + +) { $result [$matchs [2][$i]] = $matchs [3][$i]; } } return $result; } |
The Preg_match_all method of PHP holds the matching result in the third specified parameter, which is a two-dimensional array. The first dimension is an array of grouped information, where the first array holds all the matching complete strings, and the second array holds the first () worthy, and the second dimension is the value of the grouping.