preg_replace backreference date function
-
27-10-2019 - |
Question
PHP 5.2.15
I am trying to replace {date[F]} with the date function.
I have the pattern which works great.
preg_replace('/({date\[(.*?)\]})/', date(${2}), $subject);
I have tried preg_replace_callback but it doesn't seem to work even when I use create_function()
I get mostly undefined errors on my methods tried.
Solution
You do need the /e
modifier. And the replacement expression must be a string:
= preg_replace('/(\{date\[(.*?)\]})/e', 'date("$2")', $subject);
Note that you also forgot to escape the first {
curly brace.
See the manual examples for preg_replace
#105490. Or how preg_replace_callback
callbacks must unpack the match parameter (which sounds like your original issue).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow