-2

i have the following function for replace variables in a string

function replace_variables($string,$variables)
{


        return preg_replace_callback('/{\$([A-Za-z_]+)\}/', 
           create_function ('$matches', 'return $$variables[1];'), $string);
}

In php 7.2 create_function is deprecated and i dont know how to rewrite the function to work with php 5.2

Thanks

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
ntan
  • 2,195
  • 7
  • 35
  • 56
  • `function($matches) use ($variables){ return $$variables[1]; }` Use a closure. Not sure about the `$$` though (variable variable). Seems pointless in this context. – ArtisticPhoenix Jan 17 '19 at 15:56

1 Answers1

2
function replace_variables($string,$variables)
{
    return preg_replace_callback('/{\$([A-Za-z_]+)\}/',
        function ($matches) use ($variables) {
            return $$variables[1];
        }, $string);
}
Alexey
  • 3,414
  • 7
  • 26
  • 44