\d | matches a 0-9 |
##\D | except 0-9 All characters |
##\wa-zA-Z0-9_ | |
\W except All characters except 0-9A-Za-z_ | |
\s Matches all whitespace characters\n \t \r Space | |
\SMatch all non-whitespace characters | |
[ ]Specified range of atoms | |
You need to remember this, it is best to reach the dictation level. When memorizing, remember in pairs. \d matches a 0-9, then \D is all characters except 0-9.
The above has been explained very clearly. We will conduct experiments to learn these step by step.
Please be sure to reach the dictation level for these atoms when studying. Because when we do experiments in the future, you will learn it bit by bit.
\dmatches a value between 0-9
<?php
$zz = '/\d/';
$string = '我爱喝9你爱不爱喝';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
\dmatches a value other than 0-9
<?php
$zz = '/\D/';
$string = '121243中23453453';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
The match was successful and matched. Because it is not a character between 0-9.
\w Matches a-zA-Z0-9_
<?php
$zz = '/\w/';
$string = '新中_国万岁呀万岁';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
The match is successful and the underscore is matched.
\WMatch a non-a-zA-Z0-9_
<?php
$zz = '/\w/';
$string = 'afasABCWEQR44231284737';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
Match failed. Because, all the above are a-zA-Z0-9_, and there is nothing that is not a-zA-Z0-9_.
\s matches all whitespace characters\n \t \r spaces
<?php
$zz = '/\s/';
$string = "中国万
岁";
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
The match is successful because there is a carriage return.
\S Non-empty characters
<?php
$zz = '/\s/';
$string = "
a ";
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
matched successfully. Although there are spaces, carriage returns and indents on it. However, there is a non-whitespace character a. Therefore, the match is successful.
[] Specified range of atoms
<?php
$zz = '/[0-5]\w+/';
$string = '6a';
$string1 = '1C';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
Conclusion:
In the above example, 0-5 failed to match $string, while $string1 success. Because, the first value in $string is 6, which is not in the range of [0-5].
<?php
$zz = '/[a-zA-Z0-9_]\w/';
$string = 'ab';
$string1 = '9A';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
Conclusion:
$string and $string1 both match successfully. Because \w is [a-zA-Z0-9_]
<?php
$zz = '/[abc]\d+/';
$string = 'a9';
$string1 = 'b1';
$string2 = 'c5';
$string3 = 'd4';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
Conclusion:
$string, $string1, $string2 are matched successfully, but $string3 is unsuccessful. Because $string3 exceeds the range of [abc], it starts from d.
[^ character] does not match characters in the specified interval
<?php
$zz = '/[^0-9A-Za-z_]/';
$string = 'aaaaab311dd';
$string1 = '!$@!#%$#^##';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
Conclusion:
1. Matching $string failed , but it succeeds when matching $string1. Because there is a circumflex character inside the square brackets.
2.^ The function of the circumflex character inside the square brackets is not to match the characters inside the square brackets.
Summary:
Atomic | Equivalence |
---|
\w | [a-zA-Z0-9_] |
\W | [^a-zA-Z0-9_] |
\d | [0-9] |
\D | [^0-9] |
\s | [ \t\n\f\r] |
##\S | [^ \ t\n\f\r] |
Next Section<?php
$zz = '/[^0-9A-Za-z_]/';
$string = 'aaaaab311dd';
$string1 = '!$@!#%$#^##';
if(preg_match($zz, $string, $matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '没有匹配到';
}
?>
- Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...