用代码替换文本


Replacing text with code

我需要用代码替换文件中的文本。我试过用这种方法,但对我不起作用

ob_start();
include "core/menus.php";
$topmenu = ob_get_contents();
str_replace("$%TOPMENU%$", $topmenu, $template_path.'/index.tpl');
ob_end_clean();

当然,您必须更改参数顺序:D

http://www.php.net/manual/en/function.str-replace.php

并且您没有使用str_replace的结果。

如果您想获取core/menus.php的HTML,请先执行以下操作:

ob_start();
include "core/menus.php";
$top_menu = ob_get_contents();
ob_end_clean();

然后进行更换。但是,在替换任何内容之前,您需要获取模板文件的内容:

$template = file_get_contents($template_path.'/index.tpl');
$output = str_replace("$%TOPMENU%$", $topmenu, $template);
echo $output;

(我在手机上打了这个,所以请原谅任何语法错误。;)

您必须将结果分配给(F)str_replace(…)

$newMenuIndex=str_replace("$%TOPMENU%$", $topmenu, $template_path.'/index.tpl');