search
HomeBackend DevelopmentPHP ProblemWhat 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 in PHP? How is it used? (with detailed explanation)

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[&#39;str&#39;];
$pattern = &#39;/love/&#39; ;
$result = preg_match($pattern , $str , $match);
echo &#39; 匹配结果为: &#39;.$result;
echo &#39;<hr/>&#39; ;
if ( $result){ 
echo &#39;<font color="pink" size="5 ">匹配成功</font>&#39;;
}else{
echo &#39;<font color="red">匹配失败</font>&#39;;
}
var_dump($match);
?>

The code running result:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

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:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

If we add an I after the pattern modifier, we will find that the result of running the code is a successful match;

$pattern = &#39;/love/i&#39; ;

Code running results:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

If we add spaces to the string, our running results will show a matching error ;

$pattern = &#39;/lo  ve/i&#39; ;

Code running result:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

If we add an x ​​after the pattern modifier, we You will find that the result of running the code is successful matching;

$pattern = &#39;/lo  ve/ix&#39; ;

The result of running the code is:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

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 = &#39;/./ix&#39; ;

The code running result:

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

1What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

1What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

#If we want to match Chinese, we can Add a ( ) after the dot;

$pattern = &#39;/.+/ix&#39; ;

Code running result:

1What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

What are the pattern modifiers in PHP? How is it used? (with detailed explanation)

Recommended study: "

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

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

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.