The previous article introduced you to "What are metacharacters in PHP? How do we use regular expression metacharacters? (Attached code) ", this article continues to introduce to you what are the pattern modifiers in PHP? How are they used? (Detailed explanation attached), friends in need can refer to it, I hope it will be helpful to everyone.
What are the pattern modifiers:
iIgnore case
xIgnore whitespace in regular expressions
sUse the . metacharacter to match the\n
metacharacter (.) Can match any atom, except \n
m is treated as multi-line processing, and $ can match in this mode\
s (big s) Speed up matching
U mode (must remember) change greedy mode
Change greedy mode, the program defaults to greedy To match the farthest end, we can use *? in the regular to match any character while using Fidel greedy mode.
First we define $str = $_POST[] to receive the passed parameters, then we define the regular expression ($pattern), when we use two delimiters After it is defined, it is a regular expression pattern. Then, the pattern modifier at this time should be placed after the pattern modifier. Then we write the matching code, and the running result shows that the match is successful (the code is as follows)
<?php //进行匹配 $str = $_POST['str']; $pattern = '/love/' ; $result = preg_match($pattern , $str , $match); echo ' 匹配结果为: '.$result; echo '<hr/>' ; if ( $result){ echo '<font color="pink" size="5 ">匹配成功</font>'; }else{ echo '<font color="red">匹配失败</font>'; } var_dump($match); ?>
The code running result:
If we change the input string to uppercase letters, we will find that the result fails to match:
The code displays the result as follows:
If we add an I after the pattern modifier, we will find that the result of running the code is a successful match;
$pattern = '/love/i' ;
Code running results:
If we add spaces to the string, our running results will show a matching error ;
$pattern = '/lo ve/i' ;
Code running result:
If we add an x after the pattern modifier, we You will find that the result of running the code is successful matching;
$pattern = '/lo ve/ix' ;
The result of running the code is:
If I want to match everything , we can use a metacharacter (.) and this dot can match any content
Let’s take the code as an example:
$pattern = '/./ix' ;
The code running result:
#If we want to match Chinese, we can Add a ( ) after the dot;
$pattern = '/.+/ix' ;
PHP video tutorial》
The above is the detailed content of What are the pattern modifiers in PHP? How is it used? (with detailed explanation). For more information, please follow other related articles on the PHP Chinese website!