1

How can preg_replace call a function in the same class?

I have tried the following:

<?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;
        }
    }
}

But that just comes out with the following error:

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.

As soon as I outcomment this:

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

Then it works. But i need it to run a function.

And actually I don't need it to run the function with the header value in, I need the content between [template] and [/template] to be in the function.

Does anyone have any idea how to do this?

2
  • Have a look at the callback type in PHP. Commented Feb 17, 2013 at 12:23
  • So should the $replace[] be something like this? $replace[] = array_map($double, "$1");, and then above it I should have this to run my function? $call_a_template = function($a) { $this->get($a); } Commented Feb 17, 2013 at 12:42

1 Answer 1

1

I think your script might be entering an infinite loop. If you look in your get function, you're calling this:

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

So in the middle of a get call, you're pulling in the header. This then executes the exact same function which will itself pull in the header, over and over, over and over. You might want to disable this line for when $template is '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...
}

If you want to perform regular expressions, add this after the while loop:

if ($template == 'header') {
   $pattern = "/\[template\](.*?)\[\/template\]/is";
   $replace = 'WHATEVER YOU WANT TO REPLACE IT WITH';
   return preg_replace($pattern, $replace, $templates['content']);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well, it will not run the same. As the content of the header template from the databse doesn't include [template]...[/template], which is the content which should trigger this function.
$this->get is still being invoked when you define the $replace parameter, which will start an infinite loop
You was right. I just tried it out. But what if I need that to also look for [template]...[/template], as in the header there could be a [template]menu[/template]
Just do a similar preg_replace on that header template - see updated answer
Okay. Thanks. That fixed my problem :) Now, the only problem is to make it put in the real value from the $1 into the $replace[] = $this->get('header');. So that it is not always just header. Put when I put in "$1", it just comes out as $1, when it should put the content of [template]...[/template]. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.