Regular Expression Pattern modifier, regular expression Modifier
The available PCRE modifiers are listed below. The names mentioned in brackets are the names of these modifiers in PCRE. Space in the pattern modifier. line breaks are ignored. Other characters may cause errors.
-
I( PCRE_CASELESS)
-
If this modifier is set, the letters in the mode are case-insensitive.
-
M( PCRE_MULTILINE)
-
By default, PCRE considers the target string to be composed of single-line characters (however, it may actually contain multiple lines) and "first line" metacharacters (^) only match the start position of the string, while the metacharacter ($) at the end of the line matches only the end of the string or the last line break (unless DModifier ). This behavior is the same as perl. After this modifier is set, "first line" and "last line" match any line break in the target string before or after. In addition, it also matches the start and end positions of the target string respectively. This is equivalent to the/m modifier of perl. If the target string does not contain the "\ n" character, or the "^" or "$" character does not appear in the mode, setting this modifier will not affect you.
-
S( PCRE_DOTALL)
-
If this modifier is set, the dot metacharacters in the mode match all characters, including line breaks. If this modifier does not exist, the dot does not match the linefeed. This modifier is equivalent to the/s modifier in perl. An anti-character class such as [^ a] Always matches a line break, and does not rely on the modifier settings.
-
X( PCRE_EXTENDED)
-
If this modifier is set, blank data characters in the mode that are not escaped or not in the character class are always ignored, in addition, the characters between the # character and the next line break located outside of an unescaped character class are also ignored. This modifier is equivalent to the/x modifier in perl, so that the compiled mode can contain comments. Note: This is only used for data characters. Blank characters cannot appear in a special character sequence in the mode, such as a sequence (? (A condition sub-group is introduced. If there is a blank character in the Special Character Sequence defined by the syntax, compilation errors may occur. For example (? (It will cause an error ).
-
E( PREG_REPLACE_EVAL)
-
Warning
This feature wasDEPRECATEDIn PHP 5.5.0, andREMOVEDAs of PHP 7.0.0.
If this deprecated modifier is set, preg_replace () is replaced with the replacement string after being referenced, the replaced string is evaluated and executed as the php code (eval function mode), and the execution result is used as the string to be replaced. Single quotes, double quotes, and backslash ( \) And NULL characters will be escaped with a backslash (. Caution) when being replaced with a forward reference.
The addslashes () function is run on each matched backreference before the substitution takes place. as such, when the backreference is used as a quoted string, escaped characters will be converted to literals. however, characters which are escaped, which wocould normally not be converted, will retain their slashes. this makes use of this modifier very complicated.
Caution
Make sure thatreplacementThe parameter is composed of valid php code strings. Otherwise, php will produce an interpretation error on the line called by preg_replace.
Caution
Not recommendedWith this modifier, it is prone to security vulnerabilities:
<?php
$html = $_POST['html'];
// uppercase headings
$html = preg_replace(
'(
'"
$html
);
The above sample code can be used by such strings:<H1 >{$ {eval ($ _ GET [php_code]) }}. This allows attackers to execute the PHP code they want and almost completely penetrate into the server.
To prevent such remote code execution attacks, use preg_replace_callback () instead:
<?php
$html = $_POST['html'];
// uppercase headings
$html = preg_replace_callback(
'(
function ($m) {
return "
},
$html
);
Note:
Only preg_replace () uses this modifier, which is ignored by other PCRE functions.
-
A( PCRE_ANCHORED)
-
If this modifier is set, the pattern is forced to "anchored", that is, the constraint match enables it to search only from the starting position of the target string. This effect can also be constructed using the appropriate mode, and this is also the only way for perl to implement this mode.
-
D( PCRE_DOLLAR_ENDONLY)
-
If this modifier is set, the dollar sign in the schema matches only the end of the target string. If this modifier is not set, when the string ends with a linefeed, the dollar symbol will match the linefeed (but will not match any previous linefeeds ). If the modifier is set MThis modifier is ignored. There is no modifier equivalent to this modifier in perl.
-
S
-
When a mode needs to be used multiple times, it is worthwhile to spend some time to analyze it to improve the matching speed. If this modifier is set, this additional analysis will be executed. Currently, this analysis of a pattern is only applicable to non-anchoring pattern matching (that is, there is no separate fixed start character ).
-
U( PCRE_UNGREEDY)
-
This modifier reverses the "greedy" pattern of quantifiers. Make the quantifiers non-Greedy by default, followed by the quantifiers ?To make it greedy. This is incompatible with perl. It can also be set using the in-mode modifier ( ? U), Or mark it with a question mark (for example .*?).
Note:
In non-Greedy mode, it is generally not allowed to match characters that exceed pcre. backtrack_limit.
-
X( PCRE_EXTRA)
-
This modifier enables the attachment feature that is not compatible with perl. After any backslash in the mode, a character with no special meaning will cause an error, so as to retain these characters to ensure backward compatibility. By default, in perl, The backslash is followed by a character without special meaning and is considered as the original character of the character. Currently, no other features are controlled by this modifier.
-
J( PCRE_INFO_JCHANGED)
-
Set internal options (? J) modify the local PCRE_DUPNAMES. Allow sub-groups to have multiple names .)
-
U( PCRE_UTF8)
-
This modifier opens an additional feature that is incompatible with perl. Both the mode and target string are considered UTF-8. Invalid target string will cause the preg _ * function to fail to match anything. Invalid mode string will cause an error at the E_WARNING level. After PHP 5.3.4, the 5-byte and 6-byte UTF-8 character sequences were considered invalid (resp. PCRE 7.3 ). Previously it was considered invalid UTF-8.