Home >Backend Development >PHP Problem >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)

慕斯
慕斯Original
2021-06-09 16:48:541960browse

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