I'm trying to template a document, I want to replace section of the document with dynamic text by searching a document for [%text%]
and replacing it with $array['text']
or $text
. I know I can use str_replace("[%text%]", $array['text'], $page)
, but I want to find all instances of [%(.*?)%]
and replace with with $array[$1]
instead of $1
. I've tried using create_function($matches, $array)
, but it complains about Missing argument 2 for {closure}()
.
$page = preg_replace('#\[%(.*?)%\]#is', $array["$1"], $page);