Home > Article > Backend Development > Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples)
1. Understand the definition of greedy mode of regular expressions
2. Understand the definition of lazy mode of regular expressions
3. Master regular expressions Usage of greedy mode of expression
4. Master the usage of lazy mode of regular expression
5. Understand the definition of Cookie in PHP
6. Master the basics of Cookie Usage, getting and setting
The definition of greedy mode: when it can be matched or not, matching is given priority
The definition of lazy mode: Contrary to the greedy mode, when it can be matched or not, priority is given to non-matching
Example
The code is as follows:
<?php //下面的\d表示匹配数字 $p = '/\d+\-\d+/'; $str = "我的电话是010-13875678"; if( preg_match($p, $str, $match) ){ echo "字符串符合规则,匹配结果为:<br/>"; print_r( $match ); }else{ echo "字符串不符合规则"; } ?>
The running result is:
The string conforms to the rules, and the matching result is:
Array ( [0] => 010-13875678
The example code is as follows:
<?php //下面的\d表示匹配数字 $p = '/\d?\-\d?/'; $str = "我的电话是010-13875678"; if( preg_match($p, $str, $match) ){ echo "字符串符合规则,匹配结果为:<br/>"; print_r( $match ); }else{ echo "字符串不符合规则"; } ?>
The running result is:
The string conforms to the rules, and the matching result is:
Array ( [0] = > 0-1 )
It can be seen that the greedy mode is to match as many as possible, as many as possible, while the lazy mode is to match as few as possible, as few as possible
Cookie is a set of multiple sets of data, but this data is stored in the client's browser. We use Cookie to store the user's Part of the data is small in size. For example, saving the user's login account information to the client's browser
1 , to set cookies in php, use the method setcookie, but the setcookie method actually has 7 parameters, but the commonly used parameters are the following
name (Cookie name) can be passed $_COOKIE[' name'] to access
value (Cookie value)
expire (expiration time) Unix timestamp format, the default is 0, which means it will expire when the browser is closed
path (Valid path) If the path is set to '/', the entire website is valid
domain (valid domain) By default, the entire domain name is valid
2. Get a cookie in php, Pass $_COOKIE["Parameter name"]
The specific code is as follows:
Practice goal:
1. Use parameter name,value
<?php $name = "zhangsan"; setcookie("name",$name); ?>
The running result is empty
We just said that these cookies are stored in the browser, so how to check them?
The steps are as follows:
1. Press F12
2. Click Application
3. Click Cookies, Find our corresponding domain name
#So we can see the name data we set
We can see that this record actually has many attributes, In addition to name, value, there are also Domain, Path, Expires
The default path is the directory where the current page is located
Expires represents the validity period, and the default is the validity time of a session
Now let’s look at how to output this zhangsan
The specific code is as follows:
<?php $name = "zhangsan"; echo $_COOKIE["name"]; ?>
The output result is as follows:
zhangsan
Next let’s try another parameter, the valid time. We hope that the valid time of this data is 5 seconds. After it exceeds, it will become invalid.
<?php $name = "zhangsan"; setcookie("name",$name,time()+5); echo "cookie name值=".$_COOKIE["name"]; echo " 该值将在5秒钟后失效"; ?>
The running result is:
Cookie name value=zhangsan This value will expire in 5 seconds
We will run the result again after 5 seconds
The running result is:
Notice: Undefined index: name in D:\E-class\class-code\classing\index.php on line 10
cookie name value= This value will expire in 5 seconds
1. Understand the definition of greedy mode of regular expressions
2. Understand the definition of lazy mode of regular expressions
3 , Master the usage of greedy mode of regular expressions
4. Master the usage of lazy mode of regular expressions
5. Understand the definition of Cookie in PHP
6. Master Basic usage of cookies, obtaining and setting
The above is the detailed content of Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples). For more information, please follow other related articles on the PHP Chinese website!