1

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

Example:

preg_replace("/a(b?)c/e", "search_foo_term('\$1')", $str);
Toto
  • 89,455
  • 62
  • 89
  • 125
Cam
  • 14,930
  • 16
  • 77
  • 128

2 Answers2

7

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? .

Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Modifiers". – aliteralmind Apr 10 '14 at 00:35
2

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

René Höhle
  • 26,716
  • 22
  • 73
  • 82