Php extracts parameter names and parameter value instances from URLs

Source: Internet
Author: User

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:

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;

If you want to obtain all the parameter names and their corresponding values, you can also use the regular expression method. js implementation method:

The code is as follows: Copy code

Function getKeyValue (url ){
Var result = {};
Var reg = new RegExp ('([\? | &]) (. + ?) = ([^ &?] *) ', 'IG ');
Var arr = reg.exe c (url );

While (arr ){
Result [arr [2] = arr [3];

Arr = reg.exe c (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:

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 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.