PHP:在var中通过文本调用函数?

时间:2016-02-29 18:35:28

标签: php

我想为我的简单cms制作一个插件系统。如果在' page.html'中找到[call = plugin1]等文本结构,我需要执行一些代码。读取此页面的内容并将其存储在变量中。

$page_content= '<div>the result of function [call=plugin1] </div>';
 //some code to embed the result of fn 
 echo  $page_content; //finally

2 个答案:

答案 0 :(得分:1)

您可以使用str_replace:

轻松替换它
$page_content = "a bunch of stuff... [call=plugin] ... and more stuff";
$page_content = str_replace('[call=plugin]', your_plugin_function(), $page_content);
echo $page_content;

如果子字符串出现多次,您也可以使用str_replace_all。 您还可以使用多个替换来处理多个子字符串。

答案 1 :(得分:0)

我不确定我是否真的理解你的问题。是否可以将条件放在if中,如:

$page_content="";
if(call==plugin1)
    {
    $page_content= '<div>the result of function [call=plugin1] </div>';
     //some code to embed the result of fn 
    }
     echo  $page_content; //finally