PHP:在变量中通过文本调用函数


PHP: call function by text in var?

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

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

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。还可以使用多个替换来处理多个子字符串。

我不确定我是否真的理解你的问题。是否可以将条件放在 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