Re: Custom Open Tags

From: Date: Thu, 02 Dec 2004 20:50:29 +0000
Subject: Re: Custom Open Tags
Groups: php.general 
Request: Send a blank email to php-general+get-203384@lists.php.net to get a copy of this message
Hi all Thanks for your hints. I need this feature for an easy to use minimalistic CMS solution, so tweaking the PHP source is not that much of an option. But evals do the trick of course. I ruled that out at first because I thought it'll cause a lot of clumsy code. Yet there's quite an elegant way to do it, I mail it for future reference: /** * Includes a file executing <?mc ... ?> tags as if they were <?php ... ?> tags. * * @param string $file file to include * @param bool $php whether to execute <?php ... ?> tags as well */ function xinclude($file, $php=TRUE) {
    $content = file_get_contents($file);
    if (!$php) { $content = preg_replace('/<\?php.*?\?>/', '', $content); }
    $content = str_replace("<\x3Fmc", "<\x3Fphp", $content);
    $content = "print '".str_replace(array("<\x3Fphp", "\x3F>"), array("'; ", "; print '"), $content)."';";
    eval($content);
} The only downside: No single quote (') is possible outside <?xyz ... ?> tags in the file included. This can of course be fixed, but only with a ridiculous amount of code. If only there was a way to do heredoc strings that DON'T parse variables (like in Perl). -sven

« previous php.general (#203384) next »