tags:

views:

45

answers:

2
preg_replace("/{{(.*?)}}/e","$$1",$rcontent); 

Please explain the statement to me...i cant understand this

+4  A: 

Consider an example use:

$rcontent = "abc {{foo}} def";
$foo = 'bar';
$rcontent = preg_replace("/{{(.*?)}}/e","$$1",$rcontent); 
echo $rcontent; // prints abc bar def

I'm assuming that you are assigning the value of preg_match back to $rcontent or else it will not make any sense.

Now the regex you are using is {{(.*?)}} which looks for anything (non-greedyly) between {{ and }} and also remembers the matched string because of the parenthesis.
In my case the .*? matches foo.

Next the replacement part is $$1. Now $1 is foo, so $$1 will be $foo which is bar. So the {{foo}} will be replaced by value of $foo which is bar.

If the $$1 is just a type and you meant to use $1 then the regex replaces {{foo}} with foo.

codaddict
$file=file("templates/index.html");$rcontent=join("",$file);$rcontent=preg_replace("/{{(.*?)}}/e","$$1",$rcontent);print $rcontent;this is my code.its working perfectly..but templates/index.html file doesn't have any paranthesis in it..
sowmiya
@sowmiya: Sorry I did not get your question.
codaddict
am copying the content of "templates/index.html" to a php file....To copy the content am using this regex.. i don't have any curly braces in my "templates/index.html" file.. but it works perfectly..how this template engine works i dunno...
sowmiya
A: 

lazy * Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.

for eg: ".*?" matches "def" in abc "def" "ghi" jkl

[http://www.regular-expressions.info/reference.html][1]

sowmiya