Good afternoon, I am writing a template engine for my cms and faced with this question, there is code for processing functions in the template:

if (strpos($string, "{func:") !== false) { $string = preg_replace("'{func:(.+?)}(.+?){/func}'ies", "@\\1('\\2');", $string); } 

But this code processes only functions with one parameter, for example: {func: date} H: i: s {/ func}, but what if the function has not one but several parameters? For example, I write {func: date} H: i: s, {time} {/ func} This piece of code should convert time from time () to a readable form, but unfortunately only one parameter is substituted into the function and something like 14 is obtained : 49: 03,1364229666. Please help me figure it out, you need to correct the code so that it processes several parameters of the function!

I wrote this for paired parameters:

 if (strpos($string, "{funcpaire:") !== false) { $string = preg_replace("'{funcpaire:(.+?)}(.+?),(.+?){/funcpaire}'ies", "@\\1('\\2','\\3');", $string); } 

But this is not an option, because there may be more parameters!

  • Hello, I decided to make a bike, I will also make my own wheels, please tell me how to make the spokes do not bend. - zb '

2 answers 2

Get ONE parameter and then apply to it

 $parameters = explode(',',$parameter); 

Perform the pieces separately, as if they came from different {func:} {/ func} Then glue the output

 $parameter = implode(',',$parameters); 

and just replace everything together, as if there was one complex parameter.

    I did not quite understand what the "object" looks like indicating the presence of a function. But you can do this:

    {func: fname} param1 / param2 /....{/ func: fname}

    And look for such a regular season ( preg_match_all ):

    # {func \: ([a-zA-Z0-9-_] )} (. ) {func \: \ 1} #

    After iterating through the search results, calling the necessary function and using str_replace, replace the function object in the template with the result of the function called.

    I am writing a CMS myself for the needs, I had to think a lot about it)