Learn regular! Simple examples of hyperbasic

Time:2020-11-25

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

This kind of URL address appending parameter becomes: 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

This is not a big mistake, but the address bar doesn’t look good. Then what shall I do? Use the regular solution (originally I wanted to use PHP’s string processing to solve this problem, but later I think I need to learn about regularization, so I’ll use regular to do it)

The following is the background processing method I used 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; / / append all parameters to be appended first, whether they are repeated or not
If (count (expand (“& {$k} =”, $URL)) > 2) {/ / if the current URL string is divided by “& A =”, more than two split arrays are found, which indicates that the string has duplicate parameters
$url=preg_ Replace (“/ {$k} = [a-za-z0-9] * & /, ‘, $URL); / / regular replacement all” & A = x “is empty
}
}
unset($_SESSION[‘BACKURL’]);
$this->alert($msg,$url);
}

Simple explanation:
“/ {$k} = [a-za-z0-9] * & /” if we assume that $k is “a”, that is / a = [a-za-z0-9] * & / “

I don’t know if there is any misunderstanding. 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 alphabetic character, which can be understood as the repetition of the previous arbitrary character. For example, u * can be understood as: Uuuuuu… The number of u permutations is not limited, here * can be replaced by {0,}), and then followed by “&”.

This is a very simple example, but looking at the description of regular grammar on the Internet, I always feel that I can’t understand it. I don’t know whether it’s my understanding ability or the writer’s words are too profound.

In addition, 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. We should know that we put the new parameter at the end every time in our loop.

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]