开发者

Regular expression flags

Can someone explain what the 'e' flag does, or link me to somewhere that does? I couldn't find anything via google.

Example:

preg开发者_JS百科_replace("/a(b?)c/e", "search_foo_term('\$1')", $str);


e (PREG_REPLACE_EVAL)

If this 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. Only preg_replace() uses this modifier; it is ignored by other PCRE functions.

http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

So given this example:

preg_replace("/a(b?)c/e", "search_foo_term('\$1')", $str);

The replacement for the entire match will be what search_foo_term() returns when passed the match for b? .


The e flag is deprecated, mostly for security reasons. Use preg_replace_callback instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜