note 23056 added to function.eval
| From: | olivier@php.net | Date: | Mon, 08 Jul 2002 13:44:18 +0000 |
| Subject: | note 23056 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32734@lists.php.net to get a copy of this message | ||
Here an improved function of evalHTML discussed above (see brandon@louish.net or nathan vonnahme).
I do some bench with a multi-line string contain php code <?echo "hello" ?> :
for 1200 eval/replace => proceed 10 times faster
for 12 eval/replace => proceed 3 times faster
function also support both <? and <?php style... enjoy !
** BEGIN **
function eval_buffer($string) {
ob_start();
eval("$string[2];");
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
function eval_html($string) {
return preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si",
"eval_buffer",$string);
}
** END **
for older php version (< 4.0.5), it also possible to do it with preg_replace but it's a
little slower (20 %) :
** BEGIN **
function eval_buffer($string) {
ob_start();
eval(stripslashes("$string;"));
$string = ob_get_contents();
ob_end_clean();
return $string;
}
function eval_html($string) {
return preg_replace("/(<\?php|<\?)(.*?)\?>/sie",
"eval_buffer('\\2')",$string);
}
** END **
--
http://www.php.net/manual/en/function.eval.php
http://master.php.net/manage/user-notes.php?action=edit+23056
http://master.php.net/manage/user-notes.php?action=delete+23056
http://master.php.net/manage/user-notes.php?action=reject+23056