The essence of this. I have a tpl file containing the text. I include this file in php-code through the function file(); Everything is working. I start the line output of this file - it also works. Now I need a small parser, which, when it detects a special construct, will replace it with another text. Here is the text of the tpl file

 [php]peremena[/php] 

Actually php-code itself:

 $peremena = 33; //переменная для замены $php_codes = array( //собственно сами условия замены "\[php\](.+)\[/php\]" => "$1" ); $tpl_file = file('page.general.tpl'); //подгрузка файла foreach ($tpl_file as $line_num => $line){ //построчный вывод файла foreach ($php_codes as $key => $php_code) //сам парсер $line = preg_replace("#".$key."#isU", $$php_code, $line); //замена текста на переменную, имя которой содержится в $php_code echo $line; //вывод строки } 

In this example, I want to replace the text [php]peremena[/php] with the value of the $peremena variable - 33. I realized that the $php_code variable contains the text "$1" , which is the function preg_replace(); replaces the key with the text peremena . But I need that this text was displayed programmatically, like a variable, in order to substitute the values ​​of a variable with this name there. How to do it?

  • php.net/manual/ru/function.preg-replace-callback.php you are just a bad practice kit - zb '23
  • Honestly - I can not understand how this feature can help me. It just calls an anonymous function when a match is found, but I need this change to be replaced by a variable. --- I found a solution to the problem, but it is not very aesthetic, and in some moments it may even be inoperative. :( - intro94
  • it does not just call, but calls it with the parameter in the form of the found strings. but it really hardly helps you, because for some unknown reason you use variables instead of an array, make an array and then you can at least refer to it to this function. - zb '
  • like this, ideone.com/veaERl - zb '23
  • Damn it! And it works. Thanks a lot. At this stage I solved one problem. PS: why do you write the answers in the comments, and not in the form of an answer? Isn't that a plus for you? - intro94 pm

1 answer 1

Using http://www.php.net/manual/ru/function.preg-replace-callback.php

You can pass an array of variables with a parameter, but not the variable itself, like this:

 <?php $vars=array(); $vars['peremena'] = 33; //переменная для замены $vars['code'] = 'smart'; //переменная для замены $php_codes = array( //собственно сами условия замены "\[php\](.+)\[/php\]" => "$1" ); $tpl_file = [ 'Hello [php]peremena[/php], ', 'Bye [php]code[/php] code' ]; $myMatch=function($match) use ($vars) { return $vars[$match[1]]; }; foreach ($tpl_file as $line_num => $line){ //построчный вывод файла foreach ($php_codes as $key => $php_code) //сам парсер $line = preg_replace_callback("#".$key."#isU", $myMatch, $line); //замена текста на переменную, имя которой содержится в $php_code echo $line; //вывод строки } 
  • Quote: you have there a parser is not correct. I'll tell you the truth. Frankly speaking, the parser itself wrote, so I myself know that it is “that”. Can you advise what? Where are the smoking manuals? And then I thought of a site engine from scratch to write, so now I will not leave behind this idea. Yesterday I figured out classes, today, with your help, I was able to call variables. - intro94 pm
  • it is necessary to read the theory of writing parsers / interpreters, if I honestly never did this seriously (not counting the logs and other parsers), see how any template engines are made, such as github.com/bzick/fenom, at the same time, realize that this is not a very simple idea :) - zb '
  • @eicto thanks. By the way, the method that you suggested helped me a lot, but there is one thing - if I have this situation: $ vars ['peremena'] = array ('name' => 'Vasily', 'age' => 67); There is nothing to extract information from the array. There will be an inscription Array . I am in the case of writing such text in a tpl file: [php] peremena ['name'] [/ php] or [php] {peremena} ['name'] [/ php], etc. In such cases, nothing will come out at all, since he does not find the necessary changes. :) What a pity, actually. Okay. I will continue to read. Thank you again. - intro94
  • well, it’s still simple, do the square-bracket parser (from your own parser, recursively, so to speak) and if match [1] is not just a string, then start the parser again, $ vars in this case will be $ vars ['peremena'], I'm telling you, it's difficult to write a templating engine. Yes, and do not need thousands of them already. - zb '
  • @eicto I can not understand this. In the function, we get the value of the $match[1] variable. How is this value in a variable initially accepted, and under what conditions will the $match[2] variable appear? --- Yes you are right. Smarty is used in that game. True, I did not particularly go there. He really scared me, at least in a working form. - intro94 3:49 pm