The problem is, in a certain situation to give: http://jb51.net? A = 1
This type of url address appending parameter changes to: http://jb51.net? A = 1 & B = 2
But how do you know that the same parameter name already exists, for example, in this case: http://jb51.net? A = 1 & a = 2
Although there will be no major errors, it looks bad in the address bar. What should we do? Solve it with regular expressions (I wanted to solve it with php Strings, but later I thought I would use regular expressions if I had to learn them)
The following is the background processing method for returning the previous page.
Copy codeThe Code is 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 be appended, whether repeated or not
If (count (explode ("& {$ k} =", $ url)> 2) {// If the url string is separated in the format of "& a =", more than two split arrays are found, indicating that the string has repeated parameters.
$ Url = preg_replace ("/{$ k} = [a-zA-Z0-9] * &/", '', $ url); // replace all" & a = x "with null
}
}
Unset ($ _ SESSION ['backurl']);
$ This-> alert ($ msg, $ url );
}
Simple explanation:
"/{$ K} = [a-zA-Z0-9] * &/" Suppose we assume $ k = "a", that is, "/a = [a-zA-Z0-9] * &/"
I do not know whether there is a wrong understanding, this means that the match is in fact to match with the beginning of "a =", followed by any number and letter combination ([a-zA-Z0-9] *: Meaning any single digit or letter character, * can be understood as the repetition of any character above, such as u * can be understood as: uuuu ....., there is no limit on the number of u sorting, here * can be replaced by {0,}), followed "&".
This is a simple example, but I can't understand the regular expression syntax on the Internet. I don't know whether it's my understanding or whether it's too esoteric.
Also explain, why can remove repetition, the principle is very simple, http://jb51.net? When a = 1 & a = 2 is replaced with the "& a = x" format character, the last one will not be deleted, you need to know that every time in our loop, we put the new parameters at the end.