-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

1
  • function($matches) use ($variables){ return $$variables[1]; } Use a closure. Not sure about the $$ though (variable variable). Seems pointless in this context. Commented Jan 17, 2019 at 15:56

1 Answer 1

2
function replace_variables($string,$variables)
{
    return preg_replace_callback('/{\$([A-Za-z_]+)\}/',
        function ($matches) use ($variables) {
            return $$variables[1];
        }, $string);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your reply

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.