There is some html code. And in it %pools% . How to choose %pools% from a site template, and install some php code that is in some file to this place?

str_replace() does not work, I think so.


Now I implement it using explode and preg_match . Tell me something?

  • Judging by the commentary, you want to generate new PHP scripts from a certain template using PHP scripts and then execute them. If so, please describe in detail why it is needed: it is being submitted to me, the task can be solved much more simply, at the level of a regular template engine. - user6550
  • no no! I'm just wondering how people implement their template engine. - Mike
  • In template engines, there is no need to change macros to executable code (in fact, it happens, but it is so exotic that, in general, it makes no sense to mention). str_replace () on the result of the execution of some code is sufficient in over 9000 cases. - user6550
  • one
    Dig a Twig. - Arseniy


1 answer 1

str_replace is quite suitable for such a procedure. We just need to remember that he knows how to take arrays for the desired and substitution.

 str_replace($arr_search,$arr_replace,$subject); 

To facilitate the procedure, create an associative array% variable% => value, and then do this:

 str_replace(array_keys($arr_search),array_values($arr_replace),$subject); 

preg_replace apply after all replacements to clean out the residual variables that were not replaced.

  • I think str_replace is not suitable, because there should be variables for this function, and I want for example instead of% pools% to write echo 2; echo 1; echo 33; and in a file for example / data / pools.php - Mike
  • And why are you not satisfied with the variables? $ pools = '2'. '1'. '33'.file_get_contents (' pools.php '); $ pools = '2'. '1'. '33'. (include 'pools.php'); - knes