<?php $str = "可能是最好的php实用网站:http://www.80shihua.com/ ,你同意吗?'; //实用正则函数将上面网址转为大写 $pattern = '#(http://[^/]*/)#e'; $replace = 'strtoupper("$1")'; $str = preg_replace($pattern,$replace,$str); var_dump($str); // 使用修饰符e之后,就可以对匹配的网址执行PHP 函数 strtoupper() 了 结果为【string(24) "HTTP://WWW.80SHIHUA.COM/"】
正则匹配中可以实用一些PCRE修饰符,比如e(eval的意思),i(ignore-case)下面是官方文档的说明,所以不推荐使用e,可以先行正则匹配,然后使用函数
-
i (PCRE_CASELESS)
-
If this modifier is set, letters in the pattern match both upper and lower case letters.
-
m (PCRE_MULTILINE)
-
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
-
s (PCRE_DOTALL)
-
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
-
x (PCRE_EXTENDED)
-
If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include commentary inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
-
e (PREG_REPLACE_EVAL)
-
If this deprecated modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single quotes, double quotes, backslashes (\) and NULL chars will be escaped by backslashes in substituted backreferences.