Learn PHP Regular! Super Basic Simple example _ regular expression

Source: Internet
Author: User
Tags alphabetic character learn php
The problem is this, some case to give: Http://jb51.net?a=1

This type of URL address append parameter changes to: http://jb51.net?a=1&b=2

But how do you know that you already have the same parameter name, for example: http://jb51.net?a=1&a=2

This is not a big mistake, but the address bar looks bad. What about it? With the regular solution bar (originally I want to use PHP string processing solution, but later felt to learn the regular and then use the right to do it)

The following is the background processing method I used to return to the previous page

Copy Code code as follows:

function _goback ($msg =null, $get =array ()) {
$url = $_session[' Backurl '];
if ($get!=array ())
foreach ($get as $k => $g) {
$url. = ' & '. $k. ' = ' $g;//Append all parameters to append, whether or not repeated
if (Explode ("&{$k}=", $url)) (>2) {//If the current URL string is split in the format "&a=" There are more than two split arrays, indicating that the string has duplicate parameters
$url =preg_replace ("/{$k}=[a-za-z0-9]*&/", ", $url);//Replace All" &a=x "as Empty
}
}
unset ($_session[' Backurl '));
$this->alert ($msg, $url);
}

A simple explanation:
"/{$k}=[a-za-z0-9]*&/" If we assume $k= "a", that is "/a=[a-za-z0-9]*&/"

I don't know if I've got it wrong, the meaning is actually a match to "a=", followed by any number of alphanumeric combinations ([a-za-z0-9]*: meaning any single digit or alphabetic character, can be interpreted as a repetition of the preceding arbitrary character, such as u* can be understood as: uuuu ... An unlimited number of u permutations, where the * can be replaced with {0,}, followed by "&."

This is a very simple example, but look at the online description of the regular grammar, the total feeling does not understand, do not know whether I understand the ability of the problem or write people speak too abstruse.

In addition, explain why this can eliminate duplication, the principle is very simple, http://jb51.net?a=1&a=2 is replaced by "&a=x" format characters when the last one will not be deleted, you know we loop each time we are the new parameters on the last side.

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.