note 61323 added to function.eval
| From: | karig at karig dot net | Date: | Tue, 31 Jan 2006 17:10:37 +0000 |
| Subject: | note 61323 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102772@lists.php.net to get a copy of this message | ||
OK, here's what I've found to work for me:
Let's say I have a string like this, pulled from a much larger file:
$text = "<p>The following is generated by PHP:</p>\n"
. '<?php $a = 6; $b = 4; $c = $a + $b; '
. 'echo "<p>Variable c = $c</p>\n"; ?>'
. "<p>This is just more text.</p>\n";
Doing this just echoes the PHP code (so a visitor can actually read it by viewing the web
page's source) instead of executing it:
echo $text;
I wanted to have the PHP code in the text executed, so that the /result/ is echoed, and the code
itself is not. Happily, all I needed to do to get this to work for me was this:
ob_start();
eval ('?>' . $text);
$text = ob_get_clean();
// Do whatever else you want with $text before outputting it
echo $text;
That little '?>' prepended to $text (suggested by a previous note here) seems to be the
key. Note that I DON'T append a corresponding '<?php' to $text, as a previous note
suggested; I tried that, and I got an error. But I've found that "eval ('?>'
. $text);" works:
---- OUTPUT ----
The following is generated by PHP:
Variable c = 10
This is just more text.
---- HTML REVEALED IN WEB-PAGE SOURCE ----
<p>The following is generated by PHP:</p>
<p>Variable c = 10</p>
<p>This is just more text.</p>
----
Server IP: 64.246.30.37
Probable Submitter: 161.107.1.137
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61323
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61323&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61323&report=yes
Search -- http://master.php.net/manage/user-notes.php