Learn PHP! Super basic simple example

Time:2021-1-11

The problem is that, in some cases, we should give: http://jb51.net?a=1

This kind 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 http://jb51.net?a=1&a=2

There won’t be any big mistake, but the address bar doesn’t look good. Then what shall I do? Let’s use regularization (originally I wanted to use PHP’s string processing to solve this problem, but later I thought that if I wanted to learn regularization, let’s use regularization)

Here are the background processing methods I use to return to 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; / / first append all parameters to be appended, no matter whether they are repeated or not
If (count (expand (“& {$k} =”, $URL)) > 2) {/ / if the current URL string is split in the format of “& A =”, more than two split arrays are found, that means the string has duplicate parameters
$url=preg_ Replace (“/ {$k} = [a-za-z0-9] * & /”, ‘, $URL); / / regular replace all “& A = x” are empty
}
}
unset($_SESSION[‘BACKURL’]);
$this->alert($msg,$url);
}

Simple explanation:
“/ {$k} = [a-za-z0-9] * & /” suppose we assume $k = “a”, i.e. / a = [a-za-z0-9] * & / “

I don’t know if I understand it correctly. In fact, this means that the match starts with “a =”, followed by any combination of numbers and letters ([a-za-z0-9] *: it means any single number or letter character, and * can be understood as the repetition of any previous character. For example, u * can be understood as: UUUUU……, unlimited u arrangement, where * can be replaced by {0,}), and then followed by “&”.

This is a very simple example, but I can’t understand the description of regular grammar on the Internet. I don’t know whether it’s my ability to understand or whether it’s too abstruse.

In addition, explain why repetition can be removed in this way. The principle is very simple, http://jb51.net?a=1&a=2 When the “& A = x” format character is replaced, the last one will not be deleted. You know, every time in our loop, we always put the new parameter at the end.