php function Preg_replace () Prototype: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
The PHP function Preg_replace () is more powerful than the C function. The first three parameters can be used arrays, the fourth parameter $limit can set the number of substitutions, the default is to replace all. Code 6.7 is an application instance of an array substitution.
php function Preg_replace () code 6.7 array substitution
- < ? PHP
- String
- $ string = "Name: {name}< br>nemail:
{email}< br>naddress: {address}< br>n ";
- Mode
- $ Patterns = Array (
- "/{address}/",
- "/{name}/",
- "/{email}/"
- );
- Replace string
- $ Replacements = Array (
- "No.5, Wilson St., New York, U.S.A",
- "Thomas Ching",
- "Tom@emailaddress.com",
- );
- Output mode substitution results
- Print Preg_replace ($patterns,
$replacements, $string);
- ?>
The output results are as follows.
Name:thomas Ching ",
Email:tom@emailaddress.com
Address:no.5, Wilson St., New York, U.S.A
The pattern modifier "E" can be used in preg_replace regular expressions. The effect is to use the match result as an expression, and it can be re-calculated. For example:
- < ? PHP
- $ Html_body =
- " < HTML>
- < Body>
- < H1>TEST< /h1>
- My Picture < Img src= "my.gif">
- < /body >
- < /html > ";
- The HTML tags in the output will all be lowercase letters
- Echo Preg_replace (
- "/(< /?) (w+) (^>]* > )/E ",
- "' \1 '. Strtolower (' \2 '). ' \3 ' ",//mode variable here
\2 will be converted to lowercase characters by strtolower
- $html _body);
- ?>
The above is the PHP function preg_replace () of the specific use of the introduction, I hope to be helpful to everyone.
http://www.bkjia.com/PHPjc/446246.html www.bkjia.com true http://www.bkjia.com/PHPjc/446246.html techarticle php function Preg_replace () Prototype: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit]) PHP function pre G_replace () is more powerful than C. Its top three ...