3

I want to replace not only simple variables but also array ones. There is an answer of Alex Howansky about replace for simple variables:

$str = 'this is {TEST} a {THING} to test and a {array["key"]} var';

$TEST = 'one';
$THING = 'two';

$result = preg_replace('/\{([A-Z]+)\}/e', "$$1", $str);

It's work fine for {TEST} an {THING}.

For call variable variable for array I need ${$array}['key'] (https://stackoverflow.com/a/20216265/1056384). So I have tried to do second pass:

$result = preg_replace('/\{([a-z]+)\["([a-z]+)"\]\}/', "$\{$$1\}["$2"]", $result);

But in the output I have get the string $\{$array\}["key"] instead of value of $array["key"].

How to replace substrings in template with array variables?

4
  • $($array)["key"] is not valid PHP. Commented Aug 2, 2017 at 20:20
  • Normally to interpolate an array, you have to surround it in curly braces, e.g. "Your name is {$names['key']}" but I can't get the interpolation to work inside preg_replace. This is as close as I've gotten, but it's still giving me an error PHP Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '$' Maybe this will point you in the right direction: $result = preg_replace('/\{([a-z]+)\["([a-z]+)"\]\}/', "{$$1['$2']}", $result); Commented Aug 2, 2017 at 20:48
  • @AbraCadaver stackoverflow.com/a/20216265/1056384 my fault i need curly braces ${$array}['key'] Commented Aug 2, 2017 at 21:01
  • Thank you @Kita. Maybe this will work with preg_replace_callback function. Commented Aug 2, 2017 at 21:54

1 Answer 1

1

I couldn't get it to parse correctly with just preg_replace so I used a callback:

$str = 'this is {TEST} a {THING} to test and a {array["key"]} var';

$array['key'] = 'something';

$result = preg_replace_callback('/\{([a-z]+)\["([a-z]+)"\]\}/',
                                function($m) use($array){
                                    return ${$m[1]}[$m[2]];
                                },
                                $str);

If you don't know the name of the array to pass into the function you will need to make sure it's defined in the global scope and then access it that way:

$result = preg_replace_callback('/\{([a-z]+)\["([a-z]+)"\]\}/',
                                function($m) {
                                    ${$m[1]} = $GLOBALS[$m[1]];
                                    return ${$m[1]}[$m[2]];
                                },
                                $str);
Sign up to request clarification or add additional context in comments.

2 Comments

O! Cool. It's works with predefined name of $array. Can you propose a elegant solution for situation when we don't know the "array" name?
Edited with a solution if the array is global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.