An in-depth analysis of PHP regular pattern modifiers

醉折花枝作酒筹
Release: 2023-03-08 21:44:01
Original
1682 people have browsed it

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);
Copy after login

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);
Copy after login

[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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template