Extract the parameter name and value from the url

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.