2016-02-29 59 views
0

我想爲我的簡單cms做一個插件系統。如果在'page.html'中找到像[call = plugin1]這樣的文本結構,我需要執行一些代碼。該頁面的內容被讀取並存儲在變量中。PHP:通過var中的文本調用函數?

$page_content= '<div>the result of function [call=plugin1] </div>'; 
//some code to embed the result of fn 
echo $page_content; //finally 
+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如果串會出現不止一次。 您也可以使用多個替換來處理多個子字符串。

0

我不確定我是否真的瞭解您的問題。是否有可能把條件一,如果像:

$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 
+0

'if(call = plugin1)'謹慎;這是一項任務。 'if(call == plugin1)'是一個比較。按照原始http://stackoverflow.com/revisions/35707084/1 –

+0

你是對的,我只是複製/粘貼他的代碼!我糾正它! ;) –

+0

嗨,我編輯我的問題 –