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

慕斯
Release: 2023-03-10 13:00:02
Original
1746 people have browsed it

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)

' ; if ( $result){ echo '匹配成功'; }else{ echo '匹配失败'; } var_dump($match); ?>
Copy after login

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 = '/love/i' ;
Copy after login

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 = '/lo ve/i' ;
Copy after login

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 = '/lo ve/ix' ;
Copy after login

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 = '/./ix' ;
Copy after login

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 = '/.+/ix' ;
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!