Home  >  Article  >  Backend Development  >  Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples)

Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples)

易达
易达Original
2020-05-30 17:43:201996browse

Goals of this article:

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

(1) Understand the definition of greedy mode of regular expressions

The definition of greedy mode: when it can be matched or not, matching is given priority

(2) Understand the definition of lazy mode of regular expressions

The definition of lazy mode: Contrary to the greedy mode, when it can be matched or not, priority is given to non-matching

( 3) Master the usage of greedy mode of regular expressions

Example

The code is as follows:

<?php
//下面的\d表示匹配数字
$p = &#39;/\d+\-\d+/&#39;;
$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

(4) Master the usage of greedy mode of regular expressions

The example code is as follows:

<?php
//下面的\d表示匹配数字
$p = &#39;/\d?\-\d?/&#39;;
$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

(5) Understand the definition of Cookie in PHP

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

(6) Mastering the basic usage of cookies in PHP, obtaining and setting

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

Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples)

3. Click Cookies, Find our corresponding domain name

Detailed explanation of 2 modes of PHP regular expressions and cookies (code examples)

#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

Summary:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn