This function searches for all matches for the regular expression pattern in the supplied subject string and replaces each match with the string specified by the replacement parameter. If it's necessary to limit the number of replacements performed, the optional limit parameter can be supplied. If limit is omitted or is-1 all matches are replaced.
The replacement string can contain references of the form \n or $n, wheren is a number from 0 to 99. Before the actual replacement is performed, the function looks for these references in the replacement string and replaces them with the text matched by the corresponding parenthesized subpattern in the pattern parameter. \0 or$0 refers to the text matched by the whole pattern,\1 or $1 refers to the text matched by the first parenthesized subpattern, and so on.
If no matches are found in the subject string, it's returned unchanged.
Additionally, the pattern , replacement , and subject parameters can be arrays as well as strings.
If subject is an array, the search and replacement operation is performed on every element of that array, and the results are returned as an array as well.
If pattern is an array and replacement is a string, the function goes through every element of the pattern array in order and replaces all the matches for that element in the subject with the replacement string. The converse would not make sense, though, because if the pattern were a string and the replacement were an array, all the matches for the pattern would be replaced with the very first element of the replacement array and the rest would go unused.
If both pattern and replacement are arrays, the function takes an element from each array in order and performs search and replacement using them. If replacement has fewer elements than pattern , an empty string is used for the remainder of the replacement elements.
Often it may be necessary to replace the matches with some sort of dynamic expression, or to run the matches through a function before replacing them. In that case, using the /e modifier on the pattern specifies that the replacement should be treated as PHP code after the appropriate reference substitutions are done. This PHP code is evaluated and the results of the evaluation are used as the final replacement string. Make sure that the PHP code is valid syntactically, especially after the substitutions are done; otherwise, PHP complains about a parse error at the line containing the call to preg_replace() . See also the eval() function, since it's used internally to evaluate the PHP code in the replacement string, so the semantics of its execution apply here.
$text = "The definition of [recursion] may not be obvious."; echo preg_replace('/\[([^]]+)\]/', '<b>\\1</b>', $text);This example produces the following result:
The definition of <b>recursion</b> may not be obvious.
$html_body = "<Blockquote>A <B>rose</B> by any other name..<P></Blockquote>\n"; echo preg_replace ('!(</?)(\w+)([^>]*?>)!e', "'\\1'.strtolower('\\2').'\\3'", $html_body);This example produces the following output:
<blockquote>A <b>rose</b> by any other name..<p></blockquote>In this example, since the /e modifier is present, the captured tag name (\\2 reference) is fed through strtolower()() and concatenated with the other captured pieces. Note that the backslashes in the references need to be doubled because of the double quotation marks.
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.