regex - Using each match in preg_replace() in PHP -


i've read through multiple tutorials on regex , it's associated functions stumped on one.

i have really simple replace looks specific delimiter , parses name of php variable. here is:

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

i keep getting errors php not liking #1 in ${$1}. fair enough, can't start variable name number, knew that...

so tried:

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

same thing.

yet if try:

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

it works...

so, how php echo variable named whatever $1 is.

for example:

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

and output be:

helloyohelloyo 

spank you!

edit

i should mention am aware there standard recommendation on how match php variables regex, i'd working regex understand first.

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 callback on every match.

in order use $hola variable inside callback need explicitly make available inside function (use ($hola)).

all said... don't it. code php out-of-the-box.

$hola = 'yo'; $string = "hello{$hola}hello{$hola}"; echo $string; // "helloyohelloyo" 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -