用代码替换文本

时间:2014-04-12 10:02:54

标签: php

我需要用代码替换文件中的文本。 我试过使用这种方法,但它对我没用。

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

3 个答案:

答案 0 :(得分:1)

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

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

你没有使用str_replace的结果。

答案 1 :(得分:0)

如果要获取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;

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

答案 2 :(得分:0)

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

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