note 56113 added to function.eval
| From: | Nick-SafireX at osu1 dot php dot net | Date: | Tue, 23 Aug 2005 23:26:55 +0000 |
| Subject: | note 56113 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94101@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.
This is a correction of an earlier script.
In the earlier varsion the preg_replace_callback search pattern was incorrect and wouldn't
allow line breaks within the HTML sections.
I have also included a line to change shorthand <?= $var ?> to <? echo $var; ?>
This code is basicaly a version of the 'include' function which can be run on variables
instead of files. Optionaly output can be captured using output buffering.
<?
function eval_mixed_helper($arr){
return ("echo stripslashes(\"".addslashes($arr[1])."\");");
}
function eval_mixed($string){
$string = "<? ?>".$string."<? ?>";
$string = preg_replace("/<\?=\s+(.*?)\s+\?>/", "<? echo $1; ?>",
$string);
$string = str_replace('?>', '', str_replace( array('<?php',
'<?'), '', preg_replace_callback(
"/\?>((.|\n)*?)<\?/","eval_mixed_helper",$string) ) );
return eval($string);
}
// output to browser
eval_mixed($string);
// output to variable
ob_start();
eval_mixed($string);
$final_html = ob_get_clean();
?>
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56113
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56113&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56113&report=yes
Search -- http://master.php.net/manage/user-notes.php