note 55857 deleted from function.eval by peehaa
| From: | peehaa@php.net | Date: | Mon, 09 Dec 2013 12:00:25 +0000 |
| Subject: | note 55857 deleted from function.eval by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197871@lists.php.net to get a copy of this message | ||
Note Submitter: admiral [at] nuclearpixel [dot] com
----
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!