Hello. I translate sites on PHP and there such a construct is used in template engines. How do I convert it to preg_replace_callback? Already what options just did not try again

$in["#\\[index:(.+?)\\](.*?)\\[/index\\]#ies"] = "indexShow('\\1', '\\2')"; $in["#\\[modules:(.+?):(.+?)](.*?)\\[/modules]#ies"] = "modulesShow('\\1', '\\2', '\\3')"; $in["#\\[guest](.*?)\\[/guest]#ies"] = "checkGuest('\\1')"; $in["#\\[user](.*?)\\[/user]#ies"] = "checkUser('\\1')"; $in["#\\[title:(.*?)]#ies"] = "\$this->preTitle('\\1');"; $in["#\\[open](.*?)\\[/open]#ies"] = "\$this->preOpen('\\1');"; $in["#\\[userinfo:(.*?)]#ies"] = "\$this->ustinf('\\1')"; $text = preg_replace(array_keys($in), array_values($in), $str); 

Please help me to transfer from preg_replace to preg_replace_callback, I have already done various options, it does not help.

  • Actually, what is the purpose of transfer to another function? - teran
  • @teran, modifier e . - Visman
  • The purpose of the translation due to the fact that PHP 7 swears, preg_replace is outdated and says use preg_replace_callback - Denis Zakharenko
  • @DenisZakharenko, not the preg_replace() function is preg_replace() , but what I wrote in the comment above. - Visman
  • @Visman here and I did not like it :) - teran

1 answer 1

Do this for php7:

 $in["#\[index:(.+?)\](.*?)\[/index\]#is"] = function($matches) { return indexShow($matches[1], $matches[2]); }; ... $text = preg_replace_callback_array(array_keys($in), array_values($in), $str); 
  • Live and learn. To stuff a regular key with an array key is ... you need to be able to think. - rjhdby