1

どのように 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, webmaster@site.com 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.

私がこれをアウトコメントするとすぐに:

// Call a template
$pattern[] = "/\[template\](.*?)\[\/template\]/is";
$replace[] = $this->get('header');

その後、動作します。しかし、関数を実行するにはそれが必要です。

そして実際には、値を入れて関数を実行する必要はありません。関数内にとheaderの間のコンテンツが必要です。[template][/template]

誰もこれを行う方法を知っていますか?

4

1 に答える 1

1

スクリプトが無限ループに入っている可能性があると思います。get関数を見ると、これを呼び出しています。

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

したがって、get呼び出しの途中で、ヘッダーをプルしています。次に、これはまったく同じ関数を実行し、それ自体がヘッダーを何度も何度もプルします。$templateis'header'の場合、この行を無効にすることをお勧めします。

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']);
}
于 2013-02-17T12:33:48.710 に答える