This article will introduce common string Regular Expression replacement and segmentation function comparison in PHP. The following are some examples of php string Regular Expression replacement: the str_replace, str_ireplace, substr_replace, preg_replace, preg_match, preg_match_all, preg_quote, preg_split, ereg_replace, delimiter, preg_replace, and str_split functions are described below.
The relationship between the regular expression functions is ambiguous, so we can put them together for reference.
Name |
Supports Regular Expressions |
Features |
Remarks |
Str_replace |
X |
String replacement function, case sensitive |
|
Str_ireplace |
X |
String replacement function. It is case insensitive and supports array-based batch replacement. |
Thanks to franci for reminding me to add |
Substr_replace |
X |
Partially replaces the string function. You can specify the position index. |
|
|
|
|
|
Preg_replace |
Y |
Specifies the matching mode to replace, and supports sub-string reference. |
Priority |
Ereg_replace |
Y |
Replace the specified matching mode. It is case sensitive and supports substring reference. |
|
Eregi_replace |
Y |
Replace the specified matching mode, which is case insensitive and supports substring reference. |
|
|
|
|
|
Ereg |
Y |
Specifies full-text match of the pattern, which can be used for matching and judgment, or returns a matching array. |
|
Preg_match |
Y |
Specifies the exit of a pattern match, which can be used to determine whether to match or use the returned matching array. |
Priority |
Preg_match_all |
Y |
Specifies full-text match of the pattern, which is generally used to use the returned matching Array |
Priority |
|
|
|
|
Preg_split |
Y |
Specify regular Partitioning in matching mode. It is best to use explode or str_split if it can be used. |
|
Str_split |
X |
Split string with specified length. By default, a single character is split into arrays. |
|
Explode |
X |
You can specify one or more characters to split the string. If the string is successfully split, an array is returned. For example, if the string is split by 34, 12 and 5 are returned. |
|
|
|
|
|
Preg_quote |
- |
Escape Regular Expression characters, which means adding a backslash to a special character. The special characters of a regular expression include:. + *? [^] $ () {}=! <> | :- |
|
|
|
|
|
Note:
1. There are two regular expression function libraries in PHP with similar functions:
One set is provided by the POSIX (Portable Operating System Interface of Unix) Library, and the functions are named with the ereg _ prefix. Currently, POSIX regular expression function libraries are no longer recommended.
One set is provided by the PCRE (Perl Compatible Regular Expression) Library. The functions are named with the prefix. We recommend that you use them first. In PCRE, a pattern expression (that is, a regular expression) is usually included between two Backslash "/", such as the "/^ w + $/" style.
2. PHP Regular Expressions: matching, extraction, and replacement.