note 84535 deleted from function.include by peehaa

From: Date: Fri, 09 Dec 2016 22:44:47 +0000
Subject: note 84535 deleted from function.include by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208377@lists.php.net to get a copy of this message
Note Submitter: rich dot lovely at klikzltd dot co dot uk ---- I needed a way of include()ing a php page from a MySQL database. It took some work, but eventually I came up with this: <?php function include_text($text){ while(substr_count($text, '<?php') > 0){ //loop while there's code in $text list($html, $text) = explode('<?php', $text, 2); //split at first open php tag echo $html; //echo text before tag list($code, $text) = explode('?>', $text, 2); //split at closing tag eval($code); //exec code (between tags) } echo $text; //echo whatever is left } ?> It doesn't work exactly the same as include(), as newlines after the '?>' tag are echoed, rather than being discarded, but that's an exercise left to the reader to fix if they so desire, and also globals defined within the included text are not available outside the function. Not sure whether it would work with something like: <?php if($x){ ?> <p>Some HTML Output</p> ... ... <?php } else{ ?> <p>Other HTML Output</p> ... ... <?php } ?> I rarely use that, but it's easy to re-write code to avoid it using HereDoc syntax, so the example above becomes: <?php if($x){ echo <<<EOT <p>Some HTML Output</p> ... ... EOT; } else{ echo <<<EOT <p>Other HTML Output</p> ... ... EOT; } ?> Which would work with include_text() It also won't work as-is with either asp-style or short tags.

« previous php.notes (#208377) next »