$title = 123; function page_template($name, $template) { global $title; $page = file_get_contents('system/pages/'.$name); $template_file = 'system/pages/templates/'.$template; $template_open = fopen($template_file, 'r'); $template_gets = fgets($template_open, filesize($template_file)); $template_explode = explode('|', $template_gets); $template_array_chunk = array_chunk($template_explode, 2); for($i = 0; $i <= count($template_array_chunk); $i++) { $patterns_page = '/'.$template_array_chunk[$i][0].'/i'; $patterns_replace_page = $template_array_chunk[$i][1]; eval("\$patterns_replace_page = \"$patterns_replace_page\";"); $page_result = preg_replace($patterns_page, $patterns_replace_page, $page); } return $page_result; } echo page_template('main.php', 'main.dat'); 

Why does not a replacement occur? However, if return $ page_result; put in the loop itself, it will be incorrect to replace and output the same thing several times. How to solve a problem?

The main.dat file contains the following:

\ $ USERNAME \ $ | $ title |

  • And what should happen? Where does the $ template_array_chunk variable come from inside the function? Also, the function parameters are not used anywhere - $ a and $ b. - Photon
  • I did not begin to throw off the whole body of the function. And put what the problem itself. I just need to solve the problem: "However, if return $ page_result; put into the loop itself, then everything will be replaced, but output the same thing several times. How to solve the problem?" - ModaL
  • Well, in general, everything should work .. Although for complete happiness you need to see the full code of main.php and main.dat. Lay out for example on pastebin.com and throw links here By the way, using eval is a bad form. You can do everything easier - Photon
  • @Photon, main.php code - pastebin.com/nC73j4wT , main.dat code - pastebin.com/GsR5wy2z About the "easier". I would love to study everything, try to do something myself. But so far it hasn’t reached the end :( - ModaL
  • This code works with these files absolutely no problem. So now I again do not understand what the problem is :) - Photon

1 answer 1

It is unlikely that someone will answer, because it is not clear what is included and what should come out. The only thing that caught my eye was a slash before $ in the line

 eval ("\$patterns_replace_page = \"$patterns_replace_page\";"); 

If only he should stand in front of the quotes. If not, then you need to describe in more detail the situation.

  • That's right .. Answer the question, in which everything is incomprehensible, because "I did not begin to throw off the whole body of the function. And I applied what the problem was." impossible in principle - Photon
  • @ Dimka, @Photon, edited the material. Now, I think that everything is clear laid out. - ModaL
  • one
    Well, now how much I understood, some kind of material should be inserted into the place of the template, but it is not going into a heap of the $ page_result variable. It seems to me that the last line in the loop should look something like this: $ page_result = $ page_result.preg_replace ($ patterns_page, $ patterns_replace_page, $ page); - Dimka
  • one
    @ Dimka, I got it wrong .. There is a replacement in turn .. - Photon
  • one
    After $ page_result = preg_replace ($ patterns_page, $ patterns_replace_page, $ page); add $ page = $ page_result; and at the same time count ($ template_array_chunk) -1 make - Dimka