Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Function
Searches the subject for a match in pattern mode and replaces it with replacement. If limit is specified, only the limit match is replaced, and if the limit is omitted or the value is-1, all occurrences are replaced.
Replacement can contain reverse references in the form of \\n or $n, and n can represent 0 to 99,\\n text that matches the pattern nth child pattern, and \\0 represents text that matches the entire pattern.
Sub-mode
$pattern the regular expression enclosed in parentheses in the argument, the number of child patterns is the number of parentheses from left to right. (Pattern is mode)
Example
Requirement: The OT function is changed from OT (a) to OT (A, b), plus a parameter
<?php
$str = <<<eod
_article_source=ot ('. Div ' (posid (' Bd_nav ') >0) [1#].a (poshtml ('/b* ') >0) [#] ')
; _article_author=ot ('. Div (Posclass (' Doc_top p9 ') >0) [1#].a (Posclass (' B ') >0) [#] ')
EOD;
$reg = '/(=ot\ (. +) (\) [\r,\n])/';
$replacement = ' $1,n$2 ';
Echo preg_replace ($reg, $replacement, $STR);
?>
Reverse reference of PHP regular expressions and sub-schema PHP preg_replace application