0

I'm no PHP expert but I am trying the process of converting preg_replace() items with 'e' to preg_replace_callback() and found many samples that I though I understood, but apparently don't because it doesn't work. Here's one with a problem for sure:

Original:

$_rowHTML .= @preg_replace("/\[(.+)\]/Uie", "\$_callBackResult['\\1']", $_rowTemplate);

My converted version that doesn't work:

$_rowHTML .= @preg_replace_callback("/\[(.+)\]/Ui", function($m){ return $_callBackResult[$m[1]];}, $_rowTemplate);

What did I do wrong?

Thanks!!

user3161924
  • 1,849
  • 18
  • 33
  • 2
    `function($m) use ($_callBackResult) { return $_callBackResult[$m[1]];}` – Wiktor Stribiżew Nov 26 '20 at 21:53
  • @WiktorStribiżew That was the answer!! thank you! So what is the rule there, if a variable is used you have to put use in front? They have others that have `'chr(hexdec("\\1"))'` which I presume would work with the normal conversion of just `return chr(hexdec($m[1]));` – user3161924 Nov 26 '20 at 21:57
  • 1
    If you need to access variables inside the anonymous callback, pass them through `use`. – Wiktor Stribiżew Nov 26 '20 at 22:02
  • Prefect! Whoever closed this doesn't know I read the link several times before even posting a message here and no way I would have ever gotten the answer from it. The direct answer is is more valuable and clear. – user3161924 Nov 26 '20 at 23:09

0 Answers0