2013-02-17 28 views
1

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]之間的內容是在函數。

有沒有人有任何想法如何做到這一點?

+0

看一看在[回調鍵入PHP](http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback)。 – Gumbo 2013-02-17 12:23:41

+0

那麼'$ replace []'應該是這樣的嗎? '$ replace [] = array_map($ double,「$ 1」);',然後在它上面我應該有這個來運行我的函數? '$ call_a_template = function($ a){$ this-> get($ a); }' – 2013-02-17 12:42:42

回答

1

我認爲你的腳本可能會進入一個無限循環。如果您在get函數看,你打電話的:

$replace[] = $this->get('header'); 

所以在get調用的中間,你的頭拉。然後執行完全相同的功能,這個功能本身會一次又一次地拉入頭部。您可能要禁用此線時$template是「頭」:

while ($templates = mysql_fetch_array($query)) { 

    $content = $templates['content']; 

    // If this is the header, stop here 
    if ($template == 'header') 
     return $content; 

    // Rest of loop... 
} 

如果要執行正則表達式,while循環後補充一點:

if ($template == 'header') { 
    $pattern = "/\[template\](.*?)\[\/template\]/is"; 
    $replace = 'WHATEVER YOU WANT TO REPLACE IT WITH'; 
    return preg_replace($pattern, $replace, $templates['content']); 
} 
+0

好吧,它不會運行相同。由於databse中'header'模板的內容不包含'[template] ... [/ template]',這是應該觸發這個函數的內容。 – 2013-02-17 12:43:51

+1

當你定義'$ replace'參數時,'$ this-> get'仍然被調用,它將啓動一個無限循環 – hohner 2013-02-17 12:50:59

+0

你是對的。我只是試了一下。但是,如果我需要它來查找'[template] ... [/ template]',那麼在'header'中可能會出現'[template]菜單[/ template]' – 2013-02-17 13:23:31