


What are the pattern modifiers in PHP? How is it used? (with detailed explanation)
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' ;Code running result:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
