preg_replace如何在同一個類中調用一個函數?同級別的Preg_replace調用函數?
我曾嘗試以下:
<?php
defined('IN_SCRIPT') or exit;
class Templates {
protected $db;
function __construct(&$db) {
$this->db = &$db;
$settings = new Settings($this->db);
$gamebase = new Gamebase($this->db);
$this->theme = $settings->theme_id;
$this->gameid = $gamebase->getId();
}
function get($template) {
$query = $this->db->execute("
SELECT `main_templates`.`content`
FROM `main_templates`
INNER JOIN `main_templates_group`
ON `main_templates_group`.`id` = `main_templates`.`gid`
INNER JOIN `main_themes`
ON `main_themes`.`id` = `main_templates_group`.`tid`
WHERE
`main_themes`.`id` = '".$this->theme."'
&&
`main_templates`.`name` = '".$template."'
");
while ($templates = mysql_fetch_array($query)) {
$content = $templates['content'];
// Outcomment
$pattern[] = "/\/\*(.*?)\*\//is";
$replace[] = "";
// Call a template
$pattern[] = "/\[template\](.*?)\[\/template\]/is";
$replace[] = $this->get('header');
$content = preg_replace($pattern, $replace, $content);
return $content;
}
}
}
但這只是出來,出現以下錯誤:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
只要我outcomment這樣的:
// Call a template
$pattern[] = "/\[template\](.*?)\[\/template\]/is";
$replace[] = $this->get('header');
然後作品。但我需要它來運行一個函數。
而實際上我並不需要它來運行在header
值的功能,我需要[template]
和[/template]
之間的內容是在函數。
有沒有人有任何想法如何做到這一點?
看一看在[回調鍵入PHP](http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback)。 – Gumbo 2013-02-17 12:23:41
那麼'$ replace []'應該是這樣的嗎? '$ replace [] = array_map($ double,「$ 1」);',然後在它上面我應該有這個來運行我的函數? '$ call_a_template = function($ a){$ this-> get($ a); }' – 2013-02-17 12:42:42