note 84535 added to function.include
| From: | rich dot lovely at klikzltd dot co dot uk | Date: | Thu, 17 Jul 2008 20:20:42 +0000 |
| Subject: | note 84535 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141637@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 92.48.100.16
Probable Submitter: 80.229.45.56
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- https://master.php.net/note/edit/84535
Del: integrated -- https://master.php.net/note/delete/84535/integrated
Del: useless -- https://master.php.net/note/delete/84535/useless
Del: bad code -- https://master.php.net/note/delete/84535/bad+code
Del: spam -- https://master.php.net/note/delete/84535/spam
Del: non-english -- https://master.php.net/note/delete/84535/non-english
Del: in docs -- https://master.php.net/note/delete/84535/in+docs
Del: other reasons-- https://master.php.net/note/delete/84535
Reject -- https://master.php.net/note/reject/84535
Search -- https://master.php.net/manage/user-notes.php