preg_replace
(PHP 3> = 3.0.9, php 4, php 5) preg_replace - Search and replacement of regular expressions
Description Mixed
preg_replace (Mixed Pattern, Mixed Replacement, Mixed Subject [, INT LIMIT])
Search for the matching of the Pattern mode in Subject and replace it to Replacement. If LIMIT is specified, only the LIMIT match is replaced, if the limit is omitted or its value is -1, all match items will be replaced.
Replacement can contain // n or (from PHP 4.0.4) $ n-in-form, the latter is preferred. Each of these references will be replaced with text that matches the submodes in the Nth captured parentheses. N can range from 0 to 99, where // 0 or $ 0 refers to the text that matches the entire mode. The left to right is countless (starting from 1) to acquire the number of child mode from left to right.
When a replacement mode is followed by a digital (ie, immediately after a matching mode), the familiar // 1 symbol cannot be used to represent the reverse reference. For example, // 11 will make preg_replace () unclear that the reverse reference of one // 1 is followed by a number 1 or a reverse reference of one // 11. The solution in this example is to use / $ {1} 1. This forms an isolated $ 1 reverse reference, and another 1 is just a simple text.
Example 1. Reverse reference to the number of usage
PHP $ String = "April 15, 2003"; $ PATTERN = "/ (/ w ) (/ d ), (/ d ) / i"; $ replacement = "/ $ {1} 1, / $ 3"; Print preg_replace ($ Pattern, $ Replacement, $ String); / * Output ====== April1, 2003 * /?>
If a match is searched, it will return the replaced Subject, otherwise it returns the original Subject.
Each parameter of preg_replace (except for limit) can be an array. If pattern and replacement are arrays, it will be processed in the order of its key name in an array. This is not necessarily the same as the indexed number. If you use an index to identify which ofces will be replaced by which Replacement will be replaced, you should sort the array with ksort () before calling preg_replace ().
Example 2. Using an index array in preg_replace ()
PHP $ String = "The Quick Brown Fox Jumped Over The Lazy Dog."; $ patterns [0] = "/ quick /"; $ patterns [1] = "/ brown /"; $ patterns [2] = " / FOX / "; $ replacements [2] =" bear "; $ replacements [1] =" black "; $ replacements [0] =" slint preg_replace ($ patterns, $ replacements, $ string); / * Output ====== The bear black slow jumped over the lazy dog * // * By ksorting patterns and replacements, we should get what we wanted * / ksort ($ patterns);.. ksort ($ replacements); print preg_replace ($ Patterns, $ Replacements, $ String); / * Output ====== The Slow Black Bear Jumped over the lazy dog. * /?> If Subject is an array, you will perform a search for each project in Subject. And replace, and return an array.
If Pattern and Replacement are arrays, preg_replace () will take values from them to search and replace Subject, respectively. If the value in Replacement is less than the Pattern, use an empty string as the remaining replacement value. If patter is an array and Replacement is a string, each value in Pattern is used as a replacement value. In turn, there is no meaning.
/ E The modifier makes preg_replace () regard the Replacement parameter as a PHP code (after the appropriate reverse reference is replaced). Tip: To ensure that Replacement constitutes a legitimate PHP code string, PHP will restrict syntax resolution errors in the report containing the preg_replace ().
Example 3. Replace several values
php $ patterns = array ("/ (19 | 20) - (/ D {1, 2}) - (/ D {1, 2}) /", "/ ^ / s * {(/ w )} / S * = / "); $ replace = array (" // 3 /////////// 2 "," $ // 1 = "); Print preg_replace ($ Patterns, $ replace, "{startdate} = 1999-5-27");?>
This example will output:
$ strtDate = 5/27/1999
Example 4. Using / E modisor
phppreg_replace ("//?) (/ (^>] *>) / e", "'//1'.strtupper('//2').'//3'" , $ Html_body);?>
This will make all HTML tags in the input string into uppercase.
Example 5. Convert HTML into text
php // $ document should contain an HTML document. // This example will remove the HTML tag, JavaScript code // and blank characters. Some universal // HTML entities will also be converted into corresponding text. $ search = array ("'