3

I've read through multiple tutorials on regex and it's associated functions but I am stumped on this one.

I have a really simple replace that looks for a specific delimiter and parses the name of a PHP variable. Here it is:

var_dump(preg_replace('/{{\$(.*?)}}/', ${$1}, $this->file));

I keep getting errors about php not liking the #1 in ${$1}. Fair enough, can't start a variable name with a number, I knew that...

So I tried:

var_dump(preg_replace('/{{\$(.*?)}}/', ${'$1'}, $this->file));

Same thing.

Yet if I try:

var_dump(preg_replace('/{{\$(.*?)}}/', '$1 yo', $this->file));

It works...

So, how do I get php to echo a variable named whatever $1 is.

For example:

$hola = yo;
$string = hello{{$hola}}hello{{$hola}};
var_dump(preg_replace('/{{\$(.*?)}}/', ${$1}, $string));

And the output would be:

helloyohelloyo

Spank you!

EDIT

I should also mention that I am aware that there is a standard recommendation on how to match php variables with regex, but i'd like to get it working with a regex that I fully understand first.

Matthew Goulart
  • 2,873
  • 4
  • 28
  • 63

1 Answers1

3

Like so:

$hola = 'yo';
$string = 'hello{{$hola}}hello{{$hola}}';
$result = preg_replace_callback('/\{\{\$(.*?)\}\}/', function ($matches) use ($hola) {
    return ${$matches[1]};
}, $string);
var_dump($result);

preg_replace_callback calls a callback on every match.

In order to use the $hola variable inside the callback you need to explicitly make it available inside the function (use ($hola)).

All this said... I don't get it. What this code does is essentially what PHP already does out-of-the-box.

$hola = 'yo';
$string = "hello{$hola}hello{$hola}";
echo $string; // "helloyohelloyo"
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • While your answer seems to work, I think (I may be wrong) that there is a much simpler way to do it as per: http://stackoverflow.com/a/31874 and http://stackoverflow.com/a/4898239/693943. They all use preg_mach() and refer to the capture groups with $1,$2,etc... – Matthew Goulart Feb 29 '16 at 02:05
  • P.S. your example of what "php does out of the box" does NOT work... But I agree with you, it should... Is i my version of php? 5.6.17 – Matthew Goulart Feb 29 '16 at 02:08
  • @MatthewGoulart In those examples the values that are used as replacements are taken directly from the string itself. What you are trying to do is to take the values from variables *outside* the scope of the regular expression. That cannot be done simply using `preg_replace` (without using `/e`, which you should never do). – Sverri M. Olsen Feb 29 '16 at 02:10
  • @MatthewGoulart It does work, the out-of-the-box thing, but only if it is done in the PHP file. If the `$string` value comes from elsewhere (e.g. a database) then you are right, that does not work. But this is just how PHP works. – Sverri M. Olsen Feb 29 '16 at 02:12
  • Ah I see, the string comes from a file. It's for a Template engine I'm trying to build in the name of education :) – Matthew Goulart Feb 29 '16 at 02:20