Home >Backend Development >PHP Problem >An in-depth analysis of PHP regular pattern modifiers

An in-depth analysis of PHP regular pattern modifiers

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-03-22 14:56:001717browse

Today we will explain to you in detail the concepts related to PHP regular pattern modifiers. We hope it will be helpful to you. Pattern modifiers: Pattern modifiers are marked outside the entire pattern. There are several commonly used mode modifiers, namely I, m, s, and U. Let’s take a closer look at them below.

An in-depth analysis of PHP regular pattern modifiers

Modifier i

is not case-sensitive when matching, //For example: "/abc/i" can be matched with Match abc or aBC or ABc;

modifier m

Multi-line matching, if there is no "\n" character in the target string, or it does not appear in the pattern ^ or $, setting this modifier has no effect; when using this modifier, you should pay attention to: first, the target string must contain "\n", and "\n" appearing in the string means a new line First, The line break (phenomenon) seen in the Windows operating system is actually accomplished by two characters (\r\n). The line break (phenomenon) seen in the Linux operating system is accomplished through \ n to complete ; Second, ^ or $ must appear in the regular expression;

modifier s

If this modifier is set, Then all characters including newline characters will be matched;

$pattern='/t.st/s';
$str='t\nsttesttestetest';
var_dump(preg_match_all($pattern,$str,$arr));
var_dump($arr);

Modifier U

prohibits greedy matching and no repeated matching;

$pattern='/t.*t/U';
$str='t\nttestthstetelt';
var_dump(preg_match_all($pattern,$str,$arr));
var_dump($arr);

[Recommended learning: PHP video tutorial]

The above is the detailed content of An in-depth analysis of PHP regular pattern modifiers. 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