Regular replacement in php uses
preg_replace introduction
preg_replace - performs a regular expression search and replacement
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed
Search for the part of the subject that matches the pattern and replace it with replacement.
preg_replace Parameter
pattern
The pattern to search for. Can be a string or an array of strings.
Some PCRE modifiers can be used.
replacement
String or array of strings to use for replacement. If this argument is a string and pattern is an array, then all patterns are replaced with this string. If pattern and replacement are both arrays, each pattern is replaced with the corresponding element in replacement. If there are fewer elements in replacement than in pattern, the extra pattern is replaced with an empty string.
Replacement can contain back references \\n or $n, the latter is preferred grammatically. Each such reference will be replaced by the text captured by the nth matching subgroup. n can be 0-99, \\0 and $0 represent the complete pattern matching text. The serial number counting method of capturing subgroups is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use backslashes in replacement, you must use 4 ("\\\\", translator's annotation: Because this is first a PHP string, after escaping, there are two, and then after passing through the regular expression engine is considered a text backslash).
When working in replacement mode and the backreference needs to be followed by another number (for example: adding an original number immediately after a matching pattern), you cannot use the syntax \\1. Describes backreferences. For example, \\11 will make preg_replace() unable to understand whether you want a \\1 backreference followed by a source 1, or a \\11 backreference followed by nothing. The solution in this case is to use ${1}1. This creates a separate $1 backreference, a separate source1.
When using the deprecated e modifier, this function will escape some characters (ie: ', ", \ and NULL) and then perform backreference replacement. When this is done please make sure to After the reference is parsed, there are no syntax errors caused by single quotes or double quotes (for example: 'strlen(\'$1\') strlen("$2")'). Ensure that it conforms to PHP's string syntax and complies with eval syntax. Because in After completing the replacement, the engine will use the eval method to evaluate the result string as PHP code and use the return value as the final string participating in the replacement.
subject
Characters to be searched and replaced String or string array.
If subject is an array, search and replace will be performed on each element of subject, and the return value will also be an array.
limit
The maximum number of substitutions per pattern on each subject. The default is -1 (unlimited).
count
If specified, will be filled with the number of completed substitutions .
preg_replace return value
If subject is an array, preg_replace() returns an array, otherwise it returns a string.
If If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.
The above is the detailed content of How to use regular replacement in php. For more information, please follow other related articles on the PHP Chinese website!