Provides various official and user-released code examples. For code reference, you are welcome to learn how to obtain the parameter value when the parameter name is known. It is easy to use a regular expression.
I think the method of online Copy is quite good, including the js version and php version.
When the parameter name is known, it is easy to obtain the parameter value using a regular expression. The js implementation method is as follows:
function getValue(url, name) {
var reg = new RegExp('(\\?|&)' + name + '=([^&?]*)', 'i');
var arr = url.match(reg);
if (arr) {
return arr[2];
}
return null;
}
If you want to obtain all the parameter names and their corresponding values, you can also use the regular expression method. js implementation method:
function getKeyValue(url) {
var result = {};
var reg = new RegExp('([\\?|&])(.+?)=([^&?]*)', 'ig');
var arr = reg.exec(url);
while (arr) {
result[arr[2]] = arr[3];
arr = reg.exec(url);
}
return result;
}
Note that in js, another method for matching is match. match is a string method, while exec is a RegExp object method. If you use the string match method and the regular expression is specified as a global match, grouping in the regular expression is useless. The returned results are all substrings that match the regular expression. When the exec method does not use the global match flag, the first matched sub-character is returned. If the global match flag is used, the string that matches the symbol from the beginning is executed for the first time and called again, the match starts from the last matching result.
The following describes how to implement php:
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 stores the matching results in the third specified parameter, which is a two-dimensional array. The first dimension is the array of group information, that is, the first array stores all matching complete strings, and the second array stores the corresponding values of the first, the second dimension is the group value.
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB