Php extracts parameter names and parameter value instances from URLs

Source: Internet
Author: User
If I use a dynamic url, will it be followed by a. php? If a1amp; b2 is used, how can I extract parameters a and B and their values? I will introduce an instance of my own. obtain the parameter value when the parameter name is known. use the regular expression... if I use a dynamic url, it will be followed by. php? For example, a = 1 & B = 2, how do I extract the parameters a and B and their values? I will introduce an instance of my own.

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:

 

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

 

Note that in js, another method for matching is "match". "match" is a string method, while "exec" is a RegExp object method. it uses the string match method and the regular expression is specified as a global match, the group in the regular expression is useless. the returned result is all the substrings matching the regular expression. if the exec method does not use the Global match flag, the first matched substring is returned, if the global match flag is used, execute the string that matches the symbol from the beginning for the first time and call it again to start matching from the last match result.

The following provides the php implementation method. the code is as follows:

 

The preg_match_all method of php stores matching results in the third specified parameter, which is a two-dimensional array. The first dimension is an array of group information, that is, the first array stores all matched complete strings, the second array stores the values corresponding to the first (), and the second dimension is the value of the group.


Permanent link:

Reprint at will! Include the article address.

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.