Replacing {{string}} within php file

前端 未结 6 2122
栀梦
栀梦 2020-12-08 17:08

I\'m including a file in one of my class methods, and in that file has html + php code. I return a string in that code. I explicitly wrote {{newsletter}} and th

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 17:39

    This is a code i'm using for templating, should do the trick

      if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
          foreach ($m[1] as $i => $varname) {
            $template = str_replace($m[0][$i], sprintf('%s', $varname), $template);
          }
        }
    

提交回复
热议问题