This article mainly introduces the usage of PHP regular modifiers, and analyzes the functions, usage methods and related precautions of PHP regular modifiers in detail in the form of examples. Friends in need can refer to the following
Examples of this article Learned the usage of php regular modifiers. Share it with everyone for your reference, the details are as follows:
<?php //标记在整个模式之外; // 例://$mode="/\bis\b/U",其中U在外面; //修正符:i 不区分大小写的匹配; //如:"/abc/i"可以与abc或aBC或ABc等匹配; //修正符:m 将字符串视为多行,不管是那行都能匹配; // 例://模式为:$mode="/abc/m"; //要匹配的字符串为:$str="bcefg5e\nabcdfe" //注意其中\n,换行了;abc换到了下一行; //$str和$mode仍可以匹配,修正符m使得多行也可匹配; //修正符:s 将字符串视为单行,换行符作为普通字符; // 例://模式为:$mode="/pr.y/"; //要匹配字符串为:$str="pr\ny"; //两者不可匹配; . 是除了换行以外的字符可匹配; //修改下模式为:$mode="/pr.y/s"; //其中修正符s将\n视为普通字符,即不是换行; //最后两者可以匹配; //修正符:x 将模式中的空白忽略; //修正符:A 强制从目标字符串开头匹配; // 例://$mode="/abc/A"; //可以与$str="abcsdfi"匹配, //不可以与$str2="sdsdabc"匹配; //因为$str2不是以abc开头; //修正符:D 如果使用$限制结尾字符,则不允许结尾有换行; // 例://模式为:$mode="/abc$/"; //可以与最后有换行的$str="adshabc\n"匹配; //元子符$会忽略最后的换行\n; //如果模式为:$mode="/abc/D", //则不能与$str="adshabc\n"匹配, //修正符D限制其不可有换行;必需以abc结尾; //修正符:U 只匹配最近的一个字符串;不重复匹配; // 例: // 如模式为: $mode="/a.*c/"; $str="abcabbbcabbbbbc" ; preg_match($mode,$str,$content); echo $content[0]; //输出:abcabbbcabbbbbc; //如果$mode="/a.*c/";变成$mode="/a.*c/U"; // 则只匹配最近一个字符串,输出:abc; //修正符:e 配合函数preg_replace()使用, // 可以把匹配来的字符串当作正则表达式执行; ?>
Modifier:
POSIXCompatible regular expressions have no modifiers.
Perl compatible modifiers that may be used in regular expressions (spaces and newlines in the modifiers are ignored, other characters will cause errors):
i (PCRE_CASELESS):
Ignore case when matching.
m (PCRE_MULTILINE):
When this modifier is set, the start of line (^) and the end of line ($) match the beginning and end of the entire string. Also matches after and before the newline character (\n).
s (PCRE_DOTALL):
If this modifier is set, the dot metacharacter (.) in the pattern matches all characters, including newlines. Without this setting, newline characters are not included.
x (PCRE_EXTENDED):
If this modifier is set, whitespace characters in the pattern are completely ignored except those that are escaped or within a character class.
e :
If this modifier is set, preg_replace() performs the normal replacement of the backreference in the replacement string, evaluates it as PHP code, and uses The result is used to replace the searched string. Only preg_replace() uses this modifier, other PCRE functions ignore it.
A (PCRE_ANCHORED):
If this modifier is set, the pattern is forced to be "anchored", that is, it is forced to match only from the beginning of the target string.
D (PCRE_DOLLAR_ENDONLY):
If this modifier is set, the end of line ($) in the pattern only matches the end of the target string. Without this option, if the last character is a newline character, it will also be matched. This option is ignored if the m modifier is set.
S :
When a pattern will be used several times, it is worth analyzing it first in order to speed up matching. If this modifier is set additional analysis will be performed. Currently, analyzing a pattern is only useful for non-anchored patterns that do not have a single fixed starting character.
U (PCRE_UNGREEDY):
Make the default matching of "?" greedy.
X (PCRE_EXTRA): Any backslash in the
pattern followed by a letter with no special meaning results in an error, thus preserving this combination for future extensions. By default, a backslash followed by a letter with no special meaning is treated as the letter itself.
u (PCRE_UTF8):
The pattern string is treated as UTF-8.
Note:
## Pattern Modifiers
i ——Can match both uppercase and lowercase letters
M ——Treat the string as multiple lines
S ——Treat the string as a single line, and use newline characters as ordinary characters Treat, making "." match any character
X - Blanks in the pattern are ignored
U - Match the nearest string
e -Use the replaced string as an expression
phpRegular correction Detailed explanation of usage
phpRegular correctionDetailed explanation of symbol usage examples
php Regular correctionCharacter /i, /is, /s , /isU etc
The above is the detailed content of PHP regular modifier usage example analysis. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools