note 55857 added to function.eval
| From: | admiral | Date: | Mon, 15 Aug 2005 20:02:59 +0000 |
| Subject: | note 55857 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93697@lists.php.net to get a copy of this message | ||
This function will take any combination of HTML and (properly opened and closed)PHP that is given in
a string, and return a value that is the HTML and the RESULT of that PHP code and return them both
combined in the order that they were originally written.
I tried using both the eval_html(gave me carp about using 's and "s in the HTML) and
html_eval2(gave me the results of the PHP first, then all of the HTML afterwards) posted by the
other users on this function's notes, but for some reason, neither of them would really work
the way I had understood that they would work,(or in the case of some of my code, work at all)
So I combined the best of what I saw in both, and created eval_html3
<?php
function my_eval($arr) {
return ('echo stripslashes("'.addslashes($arr[0]).'");');
}
function eval_html3($string) {
$string = '<?php ?>'.$string.'<?php ?>';
$string = str_replace( '?>', '', str_replace( array( '<?php',
'<?' ), '', preg_replace_callback(
"/\?>(.*?)(<\?php|<\?)/", "my_eval", $string ) ) );
return eval($string);
}
?>
Good luck!
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55857
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55857&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55857&report=yes
Search -- http://master.php.net/manage/user-notes.php